NOT Equivalent to Array#reject!

artemave Nov 9, 2010 10 thanks

@tadman is wrong. There is a difference and, trust me, it can bite:

1.9.2 > [1,2,3,4].delete_if {|x| x > 10}

=> [1, 2, 3, 4] 1.9.2 > [1,2,3,4].reject! {|x| x > 10} => nil

That is, if reject! hasn't rejected anything, it returns nil.

RecordNotFound when any id not found

metavida Nov 3, 2010

As an example of bansalakhil's explanation.

User.find_by_id(1) #=> #<User:0x3d54a3c @attributes={"id"=>1}> User.find_by_id(2) #=> #<User:0x3d519a4 @attributes={"id"=>2}> User.find_by_id(3) #=> nil User.find_by_id(1, 2) #=> an Array with 2 User instances User.find_by_id(1, 3) #=> an ActiveRecor...

map_with_index

stevo Oct 28, 2010

Of course such a method does not exist, however we can simulate it easily

%w(a b c).to_enum(:each_with_index).map{|a,i| "#{a}, #{i}"} => ["a, 0", "b, 1", "c, 2"]

Sending array parameters

snatchev Oct 13, 2010 13 thanks

Another technique to use when you need to send an array parameter is pass in the :multiple option.

check_box("puppy", "commands", {:multiple => true}, "sit", nil)
check_box("puppy", "commands", {:multiple => true}, "fetch", nil)
check_box("puppy", "commands", {:multiple => true}, "roll_...