Disclaimer: I seem to be in a crabby mood this morning. I went back over
it
(twice) and tried to take out things that were unfriendly, sorry if I
missed
anything.
On Thu, Nov 11, 2010 at 6:10 AM, Y. NOBUOKA
[email protected]wrote:
/* obtain the Function structure which wrap the function func */
Function *func_obj = obtain_Function( func );
That isn’t what you do in Ruby. In Ruby, you don’t pass the function (or
function pointer in this case), you pass a String or Symbol.
So this example is not consistent with what Ruby does, doesn’t have any
explanatory power (that I can see, though I don’t write C extensions),
and
makes everything more complicated.
Here is an alternative model: In Ruby, everything is an object. Methods
are
objects, hence the method class.
class Method - RDoc Documentation But you can’t directly
access
them, because Ruby doesn’t make you use parentheses. So trying to access
the
method directly looks to the interpreter like you want to invoke the
method,
since that is pretty much always what you want to do. But every now and
then, you want to actually get the method, and so Ruby gives you a
getter
method that knows how to do that, and it’s aptly named “method”.
In C, that looks something like
#include <stdio.h>
#include <string.h>
typedef struct {
char* name;
int (*pointer)( int arg );
} Method;
int twice( int arg ) { return arg * 2; }
int thrice( int arg ) { return arg * 3; }
Method internal_mlist[] = {
{ “twice” , twice },
{ “thrice” , thrice },
};
Method* method( char* name ) {
int i;
for( i = 0 ; i < sizeof(internal_mlist)/sizeof(Method) ; ++i )
if( !strcmp( internal_mlist[i].name , name ) )
return &internal_mlist[i];
return NULL;
}
int main( ) {
method(“twice”);
method(“thrice”);
return 0;
}
For a longer version that is more in line with my mental model
I think that Ruby users need not know how a Method object wraps a
method, but they should know the fact that a Method object wraps a
method and a method is not a object.
Why? Is that even a Ruby thing? It seems so out of place to me that I
would
assume it is an MRI implementation detail. Do the other Rubies do it
that
way? If so, is it just for efficiency where they are all objects as soon
as
we need them to be, but keep it lazy and don’t bother turning them into
objects until such time as we know we need it?
You guys were talking about a spec earlier, the site is apparently down
right now, but I downloaded the source from github (
https://github.com/rubyspec/rubyspec), trying to find something on this.
Maybe I’m not looking in the right spot (I searched for /.method[^_s]/)
but
I didn’t see anything like this in there.
On Thu, Nov 11, 2010 at 8:59 AM, Brian C. [email protected]
wrote:
Yes. It’s important when you compare “pass by value” or “pass by
reference” in other languages. In ruby, everything is “pass by value”,
and each value is a reference to an object - even integers.
I think we settled on the phrase “object reference” last time this came
up.