how to add a "static method" dynamic ?

Discussion in 'Ruby' started by Yan Rong, Mar 14, 2007.

  1. Yan Rong

    Yan Rong Guest

    now , i know that i can use
    "define_method" add a method dynamic,
    but i want to know how to add a "static method" dynamic ?

    thanks!

    --
    Posted via http://www.ruby-forum.com/.
    Yan Rong, Mar 14, 2007
    #1
    1. Advertising

  2. > now , i know that i can use "define_method" add a method
    > dynamic, but i want to know how to add a "static method"
    > dynamic ?


    You can do this by using define_method in the context of the
    singleton class of a class:

    class << aClass
    define_method .....
    end

    In Ruby, we prefer to call this a "class method". We don't use
    the term "static method".

    gegroet,
    Erik V. - http://www.erikveen.dds.nl/

    ----------------------------------------------------------------

    ##############################################################

    class Foo
    class << self
    define_method :eek:ne do
    1
    end
    end

    class << self
    self
    end.module_eval do
    define_method :two do
    2
    end
    end
    end

    ##############################################################

    class Bar
    end

    class << Bar
    define_method :eek:ne do
    1
    end
    end

    class << Bar
    self
    end.module_eval do
    define_method :two do
    2
    end
    end

    ##############################################################

    p Foo.one
    p Foo.two

    p Bar.one
    p Bar.two

    ##############################################################

    ----------------------------------------------------------------
    Erik Veenstra, Mar 14, 2007
    #2
    1. Advertising

  3. Yan Rong

    Robert Dober Guest

    On 3/14/07, Yan Rong <> wrote:
    > now , i know that i can use
    > "define_method" add a method dynamic,
    > but i want to know how to add a "static method" dynamic ?
    >
    > thanks!
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    There are no static methods, there are only instance methods.
    But as about everything in Ruby is an Object, so is the class of your
    object and so is the singleton class of your object, maybe the
    following tricks are what you are looking for:
    707/475 > cat class.rb && ruby class.rb
    # vim: sts=2 sw=2 tw=0 expandtab nu:
    class A
    define_method :eek:ne do |p| "one(#{p})" end
    class << self
    define_method :two do |p| "two(#{p})" end
    end
    eval 'def self.three p ; "three(#{p})" end'
    end


    puts A.new.one(42)
    puts A.two(42)
    puts A.three(42)
    one(42)
    two(42)
    three(42)

    Sorry for the double post Erik but I was not about to discard all that
    hard work ;)
    Please note the ugliness of the eval for :three but it gets the job done.
    Personally I almost always use the singleton class method definition
    even when declaring statically, but I believe that it is not *exactly*
    the same thing.

    Cheers
    Robert

    --
    You see things; and you say Why?
    But I dream things that never were; and I say Why not?
    -- George Bernard Shaw
    Robert Dober, Mar 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Why do static and non-static method names collide?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    12
    Views:
    5,601
    cgbusch
    Jul 5, 2003
  2. Gary
    Replies:
    1
    Views:
    3,961
    Ryan Stewart
    Jan 16, 2004
  3. Michael
    Replies:
    3
    Views:
    4,924
    Adam Jenkins
    Jan 27, 2004
  4. G. Burton
    Replies:
    2
    Views:
    59,876
    Sudsy
    Feb 21, 2004
  5. Ryan
    Replies:
    3
    Views:
    19,534
    dingo
    Nov 17, 2004
Loading...

Share This Page