Skip to content

Instantly share code, notes, and snippets.

@SafeAF
Forked from mrb/when-proc.rb
Created August 26, 2016 22:52
Show Gist options
  • Save SafeAF/81923c3833a379342d684498200fb4e2 to your computer and use it in GitHub Desktop.
Save SafeAF/81923c3833a379342d684498200fb4e2 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