Ruby Proc Syntax

前端 未结 3 583

An answer to a question I posed yesterday on here was the following piece of Ruby code:

def overlap?(r1,r2)
  r1.include?(r2.begin) || r2.include?(r1.begin)
         


        
3条回答
  •  难免孤独
    2020-11-28 16:12

    my_method(&some_value) means to invoke my_method, passing some_value in the special argument slot, the proc-slot, usually reserved for passing do-notation blocks.

    my_block = lambda { puts "hello" }
    (1..3).each(&my_block)
    

    Any object which is a Proc or which responds to to_proc is permitted to be passed in the proc-slot. If you pass in an object which is not a Proc but which responds to to_proc, then Ruby will call to_proc on the object for you and pass the result into the method.

    The implementation of Symbol#to_proc is to return a proc which, when passed an argument, sends that argument the message that is the symbol itself. For example, :hello.to_proc.call(my_obj) will end up doing my_obj.send :hello.

    So my_array.each(&:hello) passes :hello to each in the proc-slot (where a block would normally passed, if you used the do-notation to make a block). :hello.to_proc.call(my_array[0]) ends up being my_array[0].send :hello, and the same for all subsequent indexes of my_array.

提交回复
热议问题