Created
February 19, 2012 18:44
-
-
Save georgi/1865122 to your computer and use it in GitHub Desktop.
LINQ in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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