3 ms·
What confuses me in Ruby is that functions and executable blocks (or what they are called) seem to be different things. So def my_fun(a,b,c) is different from
by Tichy 17y ago
What confuses me in Ruby is that functions and executable blocks (or what they are called) seem to be different things.
So def my_fun(a,b,c) is different from {|a,b,c|...}
I find it very confusing and as a matter of fact, I can not remember what I have to instantiate when to pass around (there is a Proc object, but does it refer to a fn or a block? And what would be the name for the other thing?).
Just a reminder that shortness is not the only criterion - it also has to be intuitive/logical.
- jfarmer 17y agoProcs, blocks, and methods are actually all different from each other. Of those three, Procs are the only ones that are actually objects (i.e., can be assigned to variables and can receive messages).
- sjs 17y agoputs_method = method(:puts) def get_block(&block) block end my_block = get_block { puts_method.call('hello!') } my_block.call #=> hello!
- tesseract 17y agoputs puts_method.class #=> Method puts my_block.class #=> Proc Method and Proc are types; blocks are a syntactic construct.
- sjs 17y agoYou're right, captured blocks are instances of Proc. However I still claim that methods are objects that can be passed around, assigned to variables, and sent messages. Method and UnboundMethod are both classes that inherit from Object. It's right there in proc.c with the Proc class. Method.ancestors #=> [Method, Object, Kernel]
- tesseract 17y agoI don't disagree with you there.
- jfarmer 17y agoDidn't know that. Ruby, you crazy.
- judofyr 17y agoThis is a method: def nuts(a, b, c) end A method can take a block (a block is not an object, it's syntax): nuts { |a, b, c| ... } You can capture a block as a Proc: foo = proc { |a, b, c| ... } foo.call(1, 2, 3) def nuts(&proc) proc.call(1, 2, 3) end nuts { |a, b, c| ... } There is also a Method object: method(:nuts).call(1, 2, 3) And you can turn a method into a Proc: method(:nuts).to_proc.call(1, 2, 3) Why does it makes sense to have these as different things? Because we want to use return in the blocks: def nuts @vars.each do |var| case var when String # Returns the method, not the block return "Test" end end end
- judofyr 17y agoAlso, they behave slightly different when it comes to arity: def nuts(a, b) end foo = Proc.new { |a, b| } nuts(1, 2, 3) # => ArgumentError: wrong number of arguments (3 for 2) foo.call(1, 2, 3) # works fine We also have lambda, which is a Proc that works (almost) as a method: http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby
- Tichy 17y agoI see the problem with return, but is it really important enough to have the confusing multitude of options? I wonder if it could at least be approximated with labels in languages without "blocks" (like break, next, retry and so on). Also, what will the return do if the block has been defined outside of the method, will it also return from the method? Anyway, thanks for the clarification. I guess to_proc was what I had been looking for back then when I ran into the problem.