14. I need to understand this


#1

Hi,

I'm looking at the code of this section, and there is one thing I don't understand: why does the batman_ironman_lambda not put out the line of victor.call?
As far as i understand it, it should run through the code and first print
"Batman will win"
and then, as it gets to the next line, print
"Ironman will win".

Anyone can explain to me why it ignores the victor.call if it's supposed to return to the method?

def batman_ironman_proc
  victor = Proc.new { return "Batman will win!" }
  victor.call
  "Iron Man will win!"
end

puts batman_ironman_proc

def batman_ironman_lambda
  victor = lambda { return "Batman will win!" }
  victor.call
  "Iron Man will win!"
end

puts batman_ironman_lambda

#2

@filiizhari

def batman_ironman_proc
  victor = Proc.new { puts "Batman will win!" }
  victor.call
  return "Iron Man will win!"
end

puts batman_ironman_proc
puts "==========="
def batman_ironman_lambda
  victor = lambda { puts "Batman will win!" }
  victor.call
  return "Iron Man will win!"
end

puts batman_ironman_lambda

http://stackoverflow.com/questions/4911353/best-explanation-of-ruby-blocks
http://stackoverflow.com/questions/626/when-to-use-lambda-when-to-use-proc-new


#3

pls some1 explian the lambda


#4

Here you go, hope this helps - i made a diagram - http://imgur.com/Z7027HQ

Further to this, i think it relates to the next exercise, in that lets say you want to search an array and return some values - if you are using a proc, it would find the first value and exit the method / whereas a lambda will search all the items of the array until the end (i believe).


#5

thanks for the diagram it's all clear :slight_smile:


#6

Thank you, that diagram really helped, but why is there no puts or print before “Iron Man will win!”