Skip to content

Instantly share code, notes, and snippets.

@VladoMS
Forked from georgi/gist:1865122
Created February 11, 2017 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VladoMS/823effe68759724b5a99015232a7b077 to your computer and use it in GitHub Desktop.
Save VladoMS/823effe68759724b5a99015232a7b077 to your computer and use it in GitHub Desktop.
LINQ in Ruby
class Query
undef select
def method_missing(id, &block)
if block
instance_variable_set("@#{id}", block)
else
instance_variable_get("@#{id}")
end
end
end
def from(enum, &block)
query = Query.new
query.instance_eval(&block)
result = enum.
select { |i| i.instance_eval(&query.where) }.
sort_by { |i| i.instance_eval(&query.order) }.
map { |i| i.instance_eval(&query.select) }
end
names = ["Alf", "Peter", "Heinz", "Udo"]
p(from(names) do
where { length == 5 }
order { downcase }
select { upcase }
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment