Skip to content

Instantly share code, notes, and snippets.

@mrb
Forked from igrigorik/when-proc.rb
Created January 9, 2011 23:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mrb/772123 to your computer and use it in GitHub Desktop.
Save mrb/772123 to your computer and use it in GitHub Desktop.
# ruby 1.9 supports 4 ways to call a proc! ex: f =->n {[:hello, n]}; f[:ruby]; f.call(:ruby); f.(:ruby)
#
# turns out, you can also call a proc via proc === :arg -- which means you can use proc's in when clauses!
# ruby doc: http://ruby-doc.org/ruby-1.9/classes/Proc.html#M001165
#
# ... kudos to @chadfowler for the tip!
#
# (note: works on 1.8.7 as well :-))
def am_i_awesome?
awesome = Proc.new do |version|
case version
when "1.9.2" then true
when "1.8.7" then false
end
end
case RUBY_VERSION
when awesome
puts "congrats, you're awesome"
else
puts "not really, you should: rvm install 1.9.2"
end
end
am_i_awesome?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment