The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Singleton aliases, C extensions

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
Singleton aliases, C extensions Posted: Oct 30, 2006 1:15 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Daniel Berger.
Original Post: Singleton aliases, C extensions
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
Creating aliases for instance methods in Ruby is clear and easy. Creating
aliases for class (singleton) methods is easy, but not as clear. Using pure
Ruby you would do something like this:
class Foo
   def self.test; "test"; end
   class << self
      alias test2 test
   end
end

Foo.test  => "test"
Foo.test2 => "test"

Doing this within a C extension is a little different. There's the
rb_define_alias() function, but no rb_define_singleton_alias() function.
Not to worry - the rb_define_alias() function takes a class or a singleton
of the class and does the right thing:
static VALUE foo_test(){ return rb_str_new2("test"); }

void Init_foo(){
   VALUE cFoo, sFoo;

   cFoo = rb_define_class("Foo", rb_cObject);
   sFoo = rb_singleton_class(cFoo);

   rb_define_alias(sFoo, "test2", "test");
}

Read: Singleton aliases, C extensions

Topic: The Impending Camping Cramping Previous Topic   Next Topic Topic: De 30 a 60 d��as para que el core de Java sea liberado Open Source

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use