Skip to content

Instantly share code, notes, and snippets.

@pjb3
Created March 20, 2013 14:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pjb3/5204928 to your computer and use it in GitHub Desktop.
Save pjb3/5204928 to your computer and use it in GitHub Desktop.
It's things like this that make Ruby fun :)
class Query
def initialize
@hash = {}
end
def method_missing(name, *args)
value = args.length > 0 ? args.first : true
@hash.merge!(name => value)
self
end
def to_hash
@hash.dup
end
end
puts Query.new.male.from('baltimore').age(greater_than: 21).in([21230, 21210]).to_hash.inspect
# => {:male=>true, :from=>"baltimore", :age=>{:greater_than=>21}, :in=>[21230, 21210]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment