DEV Community

Discussion on: Intro To Ruby Procs and Lambdas(and the difference)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited
def foo
  p = proc { puts "Proc is running!" }
  puts "The proc has been created"
  p.call
end

foo

Will output:

The proc has been created
Proc is running!

So as you can see, the proc does not run until it is called. In fact,

def foo
  return proc { puts "Proc is running!" }
end

p = foo
puts "The proc has been created, foo has returned"
p.call

will output:

The proc has been created, foo has returned
Proc is running!

Q.E.D.

Thread Thread
 
seanolad profile image
Sean

Buddy, you wrote proc wrong. It's Proc.new and writing foo doesn't call the function it passes it as an object. And of course the Proc doesn't get evaluated immediately if it's inside a function. Ruby waits until the function is called, then once it gets to the Proc it gets evaluated. Your examples don't disprove what I've said they just show how Rubies order works. I mean you literally put the proc after the puts statement in your code. And I specifically said the return statement is what you should look out for not a puts. Next time you want to try to disprove what a dev says learn the syntax first bud.

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

At first I wasn't sure if this was a troll post, but I assume it's not.

Setting aside that proc {} is syntactic sugar for Proc.new {} and definitely valid ruby code, I took your statement

Lambdas, which mind you are still nameless functions, aren't evaluated until they're called. But Procs are a class type so they're evaluated immediately.

to mean that Procs, unlike Lambdas, are evaluated as soon as they are defined, which they are not. They both get evaluated when they receive a :call signal; there's no difference whatsoever between either of them. If that wasn't what you meant, please clarify. You're not being very specific in your vocabulary, so it is difficult to know exactly what you mean and I might just be misunderstanding you.


EDIT: Some extra advise: don't tell people to "learn X". Even if you actually know what you're talking about, it makes you come across as a dick, and, if on top of it you're wrong, it makes you look even worse. Next time, either just open irb and check for yourself if something is correct, or be more open in your statement, like "I think you're wrong". Don't just assume people on the internet don't know what they're doing or it might backfire really hard.

Thread Thread
 
seanolad profile image
Sean

Okay, at this point I'm kind of out of fuel to argue so I'll set aside the argument, I didn't know that proc was synthetic sugar for Proc.new(nice) since that turns out to be true I'll read over my post and thoroughly check if what you're saying checks out. Hope I wasn't too toxic in any of my responses I get defensive sometimes. Nevertheless you're a dev, so your opinion should be heard and all warnings heeded. Again, sorry, I may have lost it.