Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlex Bishop
4,817 PointsCan I get a quick refresher on the .call method?
I most likely have this in my notes somewhere but it has been awhile and I would like some help. I want to know what exactly the .call command does in respect to calling code such as a proc or lambda. I went to the ruby doc and got the following as an answer:
call(params,...) → obj Invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics. Generates a warning if multiple values are passed to a proc that expects just one (previously this silently converted the parameters to an array). Note that prc.() invokes prc.call() with the parameters given. It’s a syntax sugar to hide “call”.
So does .call just run the code and output the values if used? Is there a simpler way to put this, or am I wrong here? I would like this in laymen's terms and explained simply if possible.
Thanks ahead of time for any help I get!
1 Answer
Ronald Jackson
9,229 PointsHi Alex -
I'll explain what the call method does by giving an example of how it works.
Let's say I write the following code:
division = Proc.new{|n| n / n}
division.call(12)
Running this code will return 1. When I invoke my proc's call method with the parameter 12, it causes the code in the block {|n| n / n} to run.
That's how the call method works with a proc. The explanation in ruby doc says what I've ilustrated in this example, but with much "bigger" words.
Hope this helps!
Best,
Ronald
Alex Bishop
4,817 PointsAlex Bishop
4,817 PointsThanks Ronald, I just needed to see it in code, I get of the just of it now.