Skip to content

Instantly share code, notes, and snippets.

@aibou
Created October 21, 2013 05:00
Show Gist options
  • Save aibou/7078875 to your computer and use it in GitHub Desktop.
Save aibou/7078875 to your computer and use it in GitHub Desktop.
Rubyのラムダ式まわり。func_with_blockあたりがクサい
def func_without_args
yield if block_given?
end
def func_with_block &block
yield block
end
def func_with_proc p
p.call
end
begin
func_without_args -> { puts "Hello" }
rescue => e
puts e #=> wrong number of arguments (1 for 0)
end
begin
func_without_args &-> { puts "Hello" } #=> Hello
rescue => e
puts e
end
begin
func_with_block -> { puts "Hello" }
rescue => e
puts e #=> wrong number of arguments (1 for 0)
end
begin
func_with_block &-> { puts "Hello" }
rescue => e
puts e #=> wrong number of arguments (1 for 0)
end
begin
func_with_proc -> { puts "Hello" } #=> Hello
rescue => e
puts e
end
begin
func_with_proc &-> { puts "Hello" }
rescue => e
puts e #=> wrong number of arguments (0 for 1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment