Ruby method, Proc, and block confusion

前端 未结 3 1202
春和景丽
春和景丽 2021-02-07 01:04

I have a couple questions about Ruby\'s methods, procedures, and blocks that strike me as rather odd. They\'re not so much about syntax or function as the logic behind the deci

3条回答
  •  失恋的感觉
    2021-02-07 01:20

    Question 1: Blocks are not objects, they are syntactic structures; this is why they cannot be assigned to a variable. This is a privilege reserved for objects.

    Question 2: Methods are not objects, so they cannot receive messages. Inversely, procs and lambdas are objects, so they cannot be invoked like methods, but must receive a message that tells them to return a value on the basis of the parameters passed with the message.

    Procs and Lambdas are objects, so they can receive the call message and be assigned to names. To summarize, it is being an object that makes procs and lambdas behave in ways you find odd. Methods and blocks are not objects and don't share that behavior.

提交回复
热议问题