I’m working on learning ruby/rails at the moment – The book i’m reading has had a tendency to skip over bits of new syntax. In particular, this bit;
person = Person.find(:first, :conditions => ["name = ?", "Mikey"])
From what I can tell this method takes a symbol as its first argument and what looks like a one-item hash with a symbol/array key/value. Is this correct and if so, why am I suddenly able to specify a hash without curly braces { } in this context?
In Ruby method parameters, any key=>value pairs that are placed after normal arguments, but before a block, are bundled into a single hash. So if you called
that means that
my_methodis being passed two arguments, a string (“hello”), and a hash ({:something=>”yo!”, :another_thing=>”bah!”}).Among other reasons, this prevents confusion between curly braces indicating a hash parameter, and curly braces indicating a block. It also makes a convenient way to simulate “named parameters”, which otherwise don’t exist in Ruby.