简体   繁体   中英

why pass block arguments to a function in ruby?

I'm unclear on why there is a need to pass block arguments when calling a function. why not just pass in as function arguments and what happens to the block arguments, how are they passed and used?

m.call(somevalue) {|_k, v| v['abc'] = 'xyz'}

module m 
  def call ( arg1, *arg2, &arg3)

  end
end

Ruby, like almost all mainstream programming languages, is a strict language, meaning that arguments are fully evaluated before being passed into the method.

Now, imagine you want to implement (a simplified version of) Integer#times . The implementation would look a little bit like this:

class Integer
  def my_times(action_to_be_executed)
    raise ArgumentError, "`self` must be non-negative but is `#{inspect}`" if negative?
    return if zero?

    action_to_be_executed

    pred.my_times(action_to_be_executed)
  end
end

3.my_times(puts "Hello")
# Hello

0.my_times(puts "Hello")
# Hello

-1.my_times(puts "Hello")
# Hello
# ArgumentError (`self` must be non-negative but is `-1`)

As you can see, 3.my_times(puts "Hello") printed Hello exactly once, instead of thrice, as it should do. Also, 0.my_times(puts "Hello") printed Hello exactly once, instead of not at all, as it should do, despite the fact that it return s in the second line of the method, and thus action_to_be_executed is never even evaluated. Even -1.my_times(puts "Hello") printed Hello exactly once, despite that fact that it raise s an ArgumentError exception as the very first thing in the method and thus the entire rest of the method body is never evaluated.

Why is that? Because Ruby is strict, Again. strict means that arguments are fully evaluated before being passed, So, what this means is that before my_times even gets called , the puts "Hello" is evaluated (which prints Hello to the standard output stream), and the result of that evaluation (which is just nil because Kernel#puts always returns nil ) is passed into the method.

So, what we need to do, is somehow delay the evaluation of the argument. One way we know how to delay evaluation, is by using a method: methods are only evaluated when they are called.

So, we take a page out of Java's playbook, and define a Single Abstract Method Protocol : the argument that is being passed to my_each must be an object which implements a method with a specific name. Let's call it call , because, well, we are going to call it.

This would look a little bit like this:

class Integer
  def my_times(action_to_be_executed)
    raise ArgumentError, "`self` must be non-negative but is `#{inspect}`" if negative?
    return if zero?

    action_to_be_executed.call

    pred.my_times(action_to_be_executed)
  end
end

def (hello = Object.new).call
  puts "Hello"
end

3.my_times(hello)
# Hello
# Hello
# Hello

0.my_times(hello)

-1.my_times(hello)
# ArgumentError (`self` must be non-negative but is `-1`)

Nice, It works! The argument that is passed is of course still strictly evaluated before being passed (we can't change the fundamental nature of Ruby from within Ruby itself), but this evaluation only results in the object that is bound by the local variable hello . The code that we want to run is another layer of indirection away and will only be executed at the point where we actually call it.

It also has another advantage: Integer#times actually makes the index of the current iteration available to the action as an argument. This was impossible to implement with our first solution, but here we can do it, because we are using a method and methods can take arguments:

class Integer
  def my_times(action_to_be_executed)
    raise ArgumentError, "`self` must be non-negative but is `#{inspect}`" if negative?

    __my_times_helper(action_to_be_executed)
  end

  protected

  def __my_times_helper(action_to_be_executed, index = 0)
    return if zero?

    action_to_be_executed.call(index)

    pred.__my_times_helper(action_to_be_executed, index + 1)
  end
end

def (hello = Object.new).call(i)
  puts "Hello from iteration #{i}"
end

3.my_times(hello)
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2

0.my_times(hello)

-1.my_times(hello)
# ArgumentError (`self` must be non-negative but is `-1`)

However, this is not actually very readable. If you didn't want to give a name to this action that we are trying to pass but instead simply literally write it down inside the argument list, it would look something like this:

3.my_times(Object.new.tap do |obj|
  def obj.call(i)
    puts "Hello from iteration #{i}"
  end
end)
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2

or on one line:

3.my_times(Object.new.tap do |obj| def obj.call; puts "Hello from iteration #{i}" end end)
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2

# or:

3.my_times(Object.new.tap {|obj| def obj.call; puts "Hello from iteration #{i}" end })
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2

Now, I don't know about you, but I find that pretty ugly.

In Ruby 1.9, Ruby added Proc literals aka stabby lambda literals to the language. Lambda literals are a concise literal syntax for writing objects with a call method, specifically Proc objects with Proc#call .

Using lambda literals, and without any changes to our existing code, it looks something like this:

3.my_times(-> i { puts "Hello from iteration #{i}" })
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2

This does not look bad!

When Yukihiro "matz" Matsumoto designed Ruby almost thirty years ago in early 1993, he did a survey of the core libraries and standard libraries of languages like Smalltalk, Scheme, and Common Lisp to figure out how such methods that take a piece of code as an argument are actually used, and he found that the overwhelming majority of such methods take exactly one code argument and all they do with that argument is call it.

So, he decided to add special language support for a single argument that contains code and can only be called . This argument is both syntactically and semantically lightweight, in particular, it looks syntactically exactly like any other control structure, and it is semantically not an object.

This special language feature, you probably guessed it, are blocks .

Every method in Ruby has an optional block parameter. I can always pass a block to a method. It's up to the method to do anything with the block. Here, for example, the block is useless because Kernel#puts doesn't do anything with a block:

puts("Hello") { puts "from the block" }
# Hello

Because blocks are not objects, you cannot call methods on them. Also, because there can be only one block argument, there is no need to give it a name: if you refer to a block, it's always clear which block because there can be only one. But, if the block doesn't have methods and doesn't have a name, how can we call it?

That's what the yield keyword is for. It temporarily "yields" control flow to the block, or, in other words, it calls the block.

With blocks , our solution would look like this:

class Integer
  def my_times(&action_to_be_executed)
    raise ArgumentError, "`self` must be non-negative but is `#{inspect}`" if negative?
    return enum_for(__callee__) unless block_given?

    __my_times_helper(&action_to_be_executed)
  end

  protected

  def __my_times_helper(&action_to_be_executed, index = 0)
    return if zero?

    yield index

    pred.__my_times_helper(&action_to_be_executed, index + 1)
  end
end

3.my_times do 
  puts "Hello from iteration #{i}"
end
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2

0.my_times do 
  puts "Hello from iteration #{i}"
end

-1.my_times do 
  puts "Hello from iteration #{i}"
end
# ArgumentError (`self` must be non-negative but is `-1`)

Okay, you might notice that I simplified a bit when I wrote above that the only thing you can do with a block is call it. There are two other things you can do with it:

  1. You can check whether a block argument was passed using Kernel#block_given? . Since blocks are always optional, and blocks have no names, there must be a way to check whether a block was passed or not.

  2. You can "roll up" a block (which is not an object and doesn't have a name) into a Proc object (which is an object) and bind it to a parameter (which gives it a name) using the & ampersand unary prefix sigil in the parameter list of the method. Now that we have an object, and a way to refer to it, we can store it in a variable, return it from a method, or (as we are doing here) pass it along as an argument to a different method, which otherwise wouldn't be possible.

    There is also the opposite operation: with the & ampersand unary prefix operator, you can "unroll" a Proc object into a block in an argument list; this makes it so that the method behaves as if you had passed the code that is stored inside the Proc as a literal block argument to the method.

And there you have it: That's what blocks are for. a semantically and syntactically lightweight form of passing code to a method.

There are other possible approaches, of course. The approach that is closest to Ruby is probably Smalltalk. Smalltalk also has a concept called blocks (in fact, that is where Ruby got both the idea and the name from). Similarly to Ruby, Smalltalk blocks have a syntactically light-weight literal form, but they are objects, and you can pass more than one to a method. Thanks to Smalltalk's generally light-weight and simple syntax, especially the keyword method syntax which intersperses parts of the method name with the arguments, even passing multiple blocks to a method call is very concise and readable.

For example, Smalltalk actually does not have an if / then / else conditional expression, in fact, Smalltalk has no control structures at all . Everything is done with methods. So, the way that a conditional works, is that the two boolean classes TrueClass and FalseClass each have a method named ifTrue:ifFalse: which takes two block arguments, and the two implementations will simply either evaluate the first or the second block. For example, the implementation in TrueClass might look a little bit like this (note that Smalltalk has no syntax for classes or methods, instead classes and methods are created in the IDE by creating class objects and method objects via the GUI):

True>>ifTrue: trueBlock ifFalse: falseBlock
   "Answer with the value of `trueBlock`."

   ↑trueBlock value

The corresponding implementation in FalseClass would then look like this:

FalseClass>>ifTrue: trueBlock ifFalse: falseBlock
   "Answer with the value of `falseBlock`."

   ↑falseBlock value

And you would call it like this:

2 < 3 ifTrue: [ Transcript show: 'yes' ] ifFalse: [ Transcript show: 'no' ].
"yes"

4 < 3 ifTrue: [ Transcript show: 'yes' ] ifFalse: [ Transcript show: 'no' ].
"no"

In ECMAScript, you can simply use function definitions as expressions, and there is also lightweight syntax for functions.

In the various Lisps, code is just data, and data is code, so you can just pass the code as an argument as data, then inside the function, treat that data as code again.

Scala has call-by-name parameters which are only evaluated when you use their name, and they are evaluated every time you use their name. It would look something like this:

implicit class IntegerTimes(val i: Int) extends AnyVal {
  @scala.annotation.tailrec
  def times(actionToBeExecuted: => Unit): Unit = {
    if (i < 0) throw new Error()
    if (i == 0) () else { actionToBeExecuted; (i - 1).times(actionToBeExecuted) }
  }
}

3.times { println("Hello") }
// Hello
// Hello
// Hello

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM