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?
Ruby program to check whether the given number is Armstrong
What are #method_missing and #send? Why are they useful?
What are the various Ruby runtimes, and how are they different?
How to create two dimensional array in ruby?
Program to Print Triangle of Numbers in Ruby
Ruby program to reverse a string
How to Replace array elements in Ruby?
Creating Array with Array.new(size, obj) in Ruby
How to Get Input with Gets in Ruby
How to add/remove elements to Array in Ruby?
What are some of your favorite gems? What are their alternatives?
How to shuffle an array in Ruby?
Ruby program to generate random numbers
Ruby program to check whether the given number is palindrome
Ruby program to print Fibonacci series
How to Sort Array in Ruby?
What's the difference between a lambda, a block and a proc?
Ruby program to check whether the given number is prime or not
Ruby program to Calculate the factorial of given number
How to get index of array element in Ruby
Ruby program to add two integer numbers
Ruby program to print an array
how to create an array with Array.[](*args) in Ruby ?