Skip to content

Instantly share code, notes, and snippets.

@jamesmartin
Created February 9, 2015 19:47
Show Gist options
  • Select an option

  • Save jamesmartin/129ebd5e99b264f7f2f8 to your computer and use it in GitHub Desktop.

Select an option

Save jamesmartin/129ebd5e99b264f7f2f8 to your computer and use it in GitHub Desktop.
Select from a list of values where arbitrary conditions are met
def has_foo?
->(thing) { thing == "foo" }
end
def has_bar?
->(thing) { thing == "bar" }
end
def has_baz?
->(thing) { thing == "baz" }
end
if __FILE__ == $0
conditions = [has_foo?, has_bar?, has_baz?]
result = ["foo", "baz", "uq_", "bar"].select do |thing|
conditions.any? { |condition| condition.call(thing) }
end
puts result
end
@jamesmartin
Copy link
Copy Markdown
Author

$ ruby callable_conditions.rb
foo
baz
bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment