Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Procs, 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).



    puts_method = method(:puts)
    def get_block(&block) block end
    my_block = get_block { puts_method.call('hello!') }
    my_block.call #=> hello!


    puts puts_method.class #=> Method
    puts my_block.class #=> Proc
Method and Proc are types; blocks are a syntactic construct.


You'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]


I don't disagree with you there.


Didn't know that. Ruby, you crazy.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: