Ruby method, Proc, and block confusion

前端 未结 3 1197
春和景丽
春和景丽 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:23

    Methods are methods — that is, they're actions that an object can take in response to messages. They are not functions.

    Blocks are closures — they're functions that close over the enclosing scope. They don't conceptually "belong to" a given object.

    In some languages, methods are merely functions that are members of an object, but Ruby does not view them this way. Separating a method from its owning object is more akin to surgery than simple assignment. Ruby takes its object-orientation model from Smalltalk, the granddaddy of modern OO.

提交回复
热议问题