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