DEV Community

loading...

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

Collapse
seanolad profile image
Sean Author

WRONG!!!! I've already read at least 3 articles on different websites(like medium)that talk about what I just wrote in this post. Maybe that's how it works in Lua(never used it), but I how I've described in my post is correct info.

Collapse
darkwiiplayer profile image
DarkWiiPlayer • 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 Author

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
DarkWiiPlayer • 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 Author

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.

Collapse
abeidahmed profile image
Abeid Ahmed

I have to agree with @darkwiiplayer on this one. As far as the syntax is concerned, procs can be created using proc, Proc.new and Kernel.proc.
As far as the differences are concerned, there are two big differences in my opinion.

  1. How the two handles the return statement.
  2. Proc can silently discard extra params and missing params are set to nil, whereas lambda complains.