proc = Proc.new {puts "hello world"} proc.class # rerturn 'Proc' lambdaWill check the number of parameters but procnot lam = lambda { |x| puts x} pro = Proc.new {|x| puts x} def lambda_test lambda_test # puts 'hello' def proc_test proc_test # return nil hello
lambda = lambda {puts "hello world"}
lambda.class # return 'Proc'
As can be seen from the above, in fact, Proc and lambda are both Proc objects.
lam.call(2) # print 2
lam.call # Argument Error: wrong number of arguments (0 for 1)
lam.call(1,2) # Argument Error: wrong number of arguments (2 for 1)
proc.call(2) # print 2
proc.call # return nil
proc.call(1,2) # print 1
lambda And proc the meaning of the 'return' key is not the same, but proc the return only call in the method body
lam = lambda { return }
lam.call
puts "hello"
end
pro = Proc.new {return}
proc.call
puts 'hello'
end
Refer: What Is the Difference Between a Block, a Proc, and a Lambda in Ruby?
How to Replace array elements in Ruby?
How to add/remove elements to Array in Ruby?
Ruby program to Calculate the factorial of given number
What are #method_missing and #send? Why are they useful?
Program to Print Triangle of Numbers in Ruby
Ruby program to generate random numbers
How to Sort Array in Ruby?
Ruby program to check whether the given number is prime or not
Ruby program to check whether the given number is palindrome
Ruby program to print an array
What are some of your favorite gems? What are their alternatives?
how to create an array with Array.[](*args) in Ruby ?
How to create two dimensional array in ruby?
How to shuffle an array in Ruby?
Ruby program to reverse a string
What's the difference between a lambda, a block and a proc?
Ruby program to add two integer numbers
How to Get Input with Gets in Ruby
Ruby program to print Fibonacci series
Ruby program to check whether the given number is Armstrong
Creating Array with Array.new(size, obj) in Ruby
How to get index of array element in Ruby
What are the various Ruby runtimes, and how are they different?