Saturday, 4 February 2012

Ruby: calling a method

Collecting hash arguments
Ruby doesn't have keyword arguments. But you can achieve the same effect by using hashes. You can place key => value pairs in an argument list, as long as they follow any normal arguments and precede any splat and block arguments. All these pairs will be collected into a single hash and passed as one argument to the method. No braces are needed. There is also the new hash literal syntax in Ruby 1.9: 
class SongList
  def search(field, params)
    # ...
  end
end 

list.search(:title, genre: 'jazz', duration_less_than: 270)

No comments :

Post a Comment