Feeds:
Posts
Comments

Archive for the ‘Ruby’ Category

Using ensure to reset a value

I have a bunch of links I need to build on page, links that are a modification to the current path. I wanted to be able to do my modifications and return the modified path (@updated_path) without changing the original path variable (@path).
This isn’t difficult stuff, the only thing that I had to ponder [...]

Read Full Post »

Ruby: inject

Inject is a powerfully cool method provided by the Enumerable module. At first I was a little confused by the name. I didn’t know what it meant. Here’s a simple example:
an_array = [1, 2, 3, 4, 5, 6]

def x10(v)
v * 10
end

an_array.inject([]) do |sum, num|
sum << x10(num)
end

=> [10, 20, [...]

Read Full Post »

Matz Google Tech Talk

Here are some notes from the talk Matz gave. It doesn’t every little detail of the whole ~31 minute talk with a ~19 minute question/answer session. Just some of the points I wanted to remember.
1.9 Incompatibilities with 1.8
block parameters local variables only in pipe blocked scoped #The following will not work in 1.9 # [...]

Read Full Post »

collect and flatten

I run into this scenario many times and never really thought about changing it until recently. It’s very common, I have an array of values and I want to perform an action on each item. My return value would be an array of the results from the action.
Here’s a sample lookup that most [...]

Read Full Post »

exists? == not_nil?

Apparently Facets adds the exists? functionality in the form of not_nil?. It seems there’s a lot of goodness in Facets that I will now have to investigate. Thanks to Trans for giving me the heads up.
Here’s to not re-inventing the wheel. Cheers.

Read Full Post »

exists?

I like readable code is it’s one of the nice attributes of Ruby. Today I had some code that could use some improvement.
Ruby has a method called nil? that you can send to any object. When I want to test is that object is not nil? I could:
#use unless unless [...]

Read Full Post »

Mikey

So, I have a site that synchronizes data with another server. It does this via after_create, after_update and after_delete triggers on some models. All of those triggers use the same method that returns a connection object which has various methods such as:
# SyncServer::connection method module SyncServer def [...]

Read Full Post »

nearest power of 2

This is one of the reasons I’m a fan of Ruby:
class << Math def log2(n) log(n) / log(2) end end

class Integer def nearest_power_of_2 2**(Math.log2(self).floor) end end
47.nearest_power_of_2 => [...]

Read Full Post »

gem server

In the category of why I haven’t I used this yet (duh!):
run: gem_serverto run as daemon: gem_server –daemon
then access: localhost:8808
Documentation for your installed gems. Nice.
If you don’t have any documentation, run: gem rdoc –all
This will generate the docs for all the gems installed on your system.
Note: gem_server has been deprecated in favor of [...]

Read Full Post »

Interview questions/answers for an entry level Ruby developer.(Use these questions in conjunction with the 101 level Rails questions)
Q) How do you comment out a block of code?
A) Use =begin and =end.
=begin def my_commented_out_method end =end
You could use successive # signs, but that’s just tedious:## def my commented_out_method# end#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) How do [...]

Read Full Post »