I'm new to programming, and ruby is my first real run at it. I get blocks, but procs seem like a light method/function concept -- why use them? Why not just use a method?
Thanks for your help.
I'm new to programming, and ruby is my first real run at it. I get blocks, but procs seem like a light method/function concept -- why use them? Why not just use a method?
Thanks for your help.
Proc is a callable piece of code. You can store it in a variable, pass as an argument and otherwise treat it as a first-class value.
Why not just use a method?
Depends on what you mean by "method" here.
class Foo
def bar
puts "hello"
end
end
f = Foo.new
In this code snippet usage of method bar
is pretty limited. You can call it, and that's it. However, if you wanted to store a reference to it (to pass somewhere else and there call it), you can do this:
f = Foo.new
bar_method = f.method(:bar)
Here bar_method
is very similar to lambda (which is similar to Proc). bar_method
is a first-class citizen, f.bar
is not.
For more information, read the article mentioned by @minitech.
self
(or whose self
wouldn't be the right thing). The whole Method/Proc/lambda/block thing in Ruby is a bit of a mess IMO. I'm having a hard time thinking of a good answer that wouldn't end up being an entire chapter of a book. –
Entoil Why use procs instead of methods?
A common design pattern involves choosing a method or code block to call based on a runtime value. For example...
case 1
when 0
p :a
when 1
p :b
when 2
p :c
end
This gets kind of clunky when there are many selectors and there is no way to put the dispatch mechanism together incrementally. So instead something like this can be done:
h = [ proc { p :a }, proc { p :b }, proc { p :c } ]
h[1].call
You can also use a Hash
instead of an Array
if your keys are not a sequence of small integers. Although the clunky case-selector design pattern occurs frequently in all languages, the dispatch-table-of-procs is rarely used. Usually, it's possible to store the results themselves in the Array
or Hash
and then just index them directly. But for something complicated, calling a proc
allows the most flexibility.
As you proceed in Ruby you will find that this is the reason Ruby has blocks. Blocks are essentially methods that have been passed as a parameter to another method. This is so easy to do in Ruby and Smalltalk that it gets used all the time. You can do the same thing in C but it's too awkward to be any fun and so it is only seen in C when the code writer is losing a desperate battle with complexity.
© 2022 - 2024 — McMap. All rights reserved.
's'.method(:size)
can be stored in a variable, passed as an argument, and otherwise treated as a first class value but it is a method while not being a Proc. – Entoil