Skip to content

Instantly share code, notes, and snippets.

@harley
Last active December 27, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harley/7314134 to your computer and use it in GitHub Desktop.
Save harley/7314134 to your computer and use it in GitHub Desktop.
.0.0-p247 :074 > class Q
2.0.0-p247 :075?> def test
2.0.0-p247 :076?> puts "no block. but you can pass a block to me"
2.0.0-p247 :077?> end
2.0.0-p247 :078?> def btest(&block)
2.0.0-p247 :079?> puts "with block"
2.0.0-p247 :080?> block.call
2.0.0-p247 :081?> end
2.0.0-p247 :082?> def ctest
2.0.0-p247 :083?> if block_given?
2.0.0-p247 :084?> yield
2.0.0-p247 :085?> else
2.0.0-p247 :086 > puts "no block was passed"
2.0.0-p247 :087?> end
2.0.0-p247 :088?> end
2.0.0-p247 :089?> end
=> nil
2.0.0-p247 :090 > Q.new.test
no block. but you can pass a block to me
=> nil
2.0.0-p247 :091 > Q.new.test {puts "test"}
no block. but you can pass a block to me
=> nil
2.0.0-p247 :092 > Q.new.btest {puts "block code"}
with block
block code
=> nil
2.0.0-p247 :093 > Q.new.btest
with block
NoMethodError: undefined method `call' for nil:NilClass
from (irb):80:in `btest'
from (irb):93
from /Users/dtt22/.rvm/gems/ruby-2.0.0-p247@daily_themes/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/dtt22/.rvm/gems/ruby-2.0.0-p247@daily_themes/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/dtt22/.rvm/gems/ruby-2.0.0-p247@daily_themes/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.0.0-p247 :094 > Q.new.ctest {puts "block code"}
block code
=> nil
2.0.0-p247 :095 > Q.new.ctest
no block was passed
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment