Drop stuff in this thing’s stuff to do stuff.
The fact that the extension API for this is based on ruby is making me want to buy it.
Sounds like the tool I want to use!
Drop stuff in this thing’s stuff to do stuff.
The fact that the extension API for this is based on ruby is making me want to buy it.
Let’s say you have
named_scope :red, :conditions => {:color => 'red'}Do not do this:
Shirt.red(:limit => 1) #=> ALL red shirtsYou will get all red shirts. I don’t care how many you limit to, you will get them all. Instead, you need to chain using
find(:all)or it’s aliasall:Shirt.red.all(:limit => 1) #=> ONE red shirtThis has been your Rails public service announcement for 17 September 2008.
Wouldn’t it be easier to do this:?
Shirt.red.first/last
Compare:
> def double(n) > n * 2 > end > (1..5).map {|n| double(n)} => [2, 4, 6, 8, 10] > (1..5).map &method(:double) => [2, 4, 6, 8, 10]Explanation:
methodreturns aMethodobject representing the named method.Methoddefinesto_procto return theProccorresponding to the method. And we know that our trusty friend&tries to convert the object to aProcusing it’sto_procmethod.(via Giles Bowkett)
May as well add into this:
> (1..5).map {|n| n.double}
=> [2, 4, 6, 8, 10]
> (1..5).map {&double}
=> [2, 4, 6, 8, 10]
I generally prefer to use collect over map. It gives a better idea of what the method does, then again it is 4 characters longer :P.