Skip to content

Instantly share code, notes, and snippets.

@glebm
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glebm/11230676 to your computer and use it in GitHub Desktop.
Save glebm/11230676 to your computer and use it in GitHub Desktop.
make all Ruby things Enumerable
class Object
def each(&block)
return to_enum(:each) { 1 } unless block
[self].each(&block)
end
include Enumerable
end
class NilClass
def each(&block)
return to_enum(:each) { 0 } unless block
end
end
#---
def greetings(users)
users.map { |user| "Hello, #{user}" }
end
greetings %w(Finn Jake) #=> ["Hello, Finn", "Hello, Jake"]
greetings 'Finn' #=> ["Hello, Finn"]
greetings nil #=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment