+ 5
What is the difference between lambdas and procs?
2 Antworten
+ 9
proc = Proc.new do |x|
puts "Hi #{x}"
end
lamb = lambda {puts "Hi #{x}"}
proc.call "Bob"
#Outputs: Hi Bob
lamb.call "David"
#Outputs: Hi David
proc.call
#Outputs: Hi
lamb.call
#Outputs: Error given arguement(Expected 1, given 0)
#Both are the same but when proc expects an arguement but you gave none, then it just outputs the Proc and a nil value. However in lambda when it expects an arguement and you give nothing then it outputs an error.
0
We create lambda when we only need a single line code for make a simple function