alias to module methods or class methods

Discussion in 'Ruby' started by Geert Fannes, Apr 4, 2005.

  1. Geert Fannes

    Geert Fannes Guest

    ------_=_NextPart_001_01C53906.28EDE438
    Content-Type: text/plain;
    charset="us-ascii"
    Content-Transfer-Encoding: quoted-printable

    Hello,

    I tried to fix the Math.sqrt() domain error on windows I reported an
    hour ago by redefining the Math.sqrt method:

    module Math

    alias sqrt_old sqrt

    def Math.sqrt(x)

    begin

    Math.sqrt_old(x)

    rescue Errno::EDOM

    return 0.0/0.0

    end

    end

    end

    =20

    But this does not work. Apparently, I cannot create an alias to a module
    method. I tested a bit and found out I could not create aliases to class
    functions either. How can I create aliases to module methods and class
    methods?

    =20

    Greetings,

    Geert.

    PS:I found some confusing thread about creating class method aliases so
    I ask it here again


    ------_=_NextPart_001_01C53906.28EDE438--
     
    Geert Fannes, Apr 4, 2005
    #1
    1. Advertising

  2. Hi --

    On Mon, 4 Apr 2005, Geert Fannes wrote:

    > Hello,
    >
    > I tried to fix the Math.sqrt() domain error on windows I reported an
    > hour ago by redefining the Math.sqrt method:
    >
    > module Math
    >
    > alias sqrt_old sqrt
    >
    > def Math.sqrt(x)
    >
    > begin
    >
    > Math.sqrt_old(x)
    >
    > rescue Errno::EDOM
    >
    > return 0.0/0.0
    >
    > end
    >
    > end
    >
    > end
    >
    >
    >
    > But this does not work. Apparently, I cannot create an alias to a module
    > method. I tested a bit and found out I could not create aliases to class
    > functions either. How can I create aliases to module methods and class
    > methods?


    You have to make the change in the class where the method is defined.
    Class and module methods are basically singleton methods -- that is,
    methods defined inside the singleton class of a particular object. In
    this case, the object is the Module object "Math" -- so you need to
    open up the singleton class of Math:

    class << Math
    alias sqrt_old sqrt
    def sqrt(x)
    Math.sqrt_old(x)
    rescue Errno::EDOM
    return 0.0/0.0
    end
    end

    (Note that a method definition gives you an implicit begin/end block,
    so you don't need to write one inside the method.)


    David

    --
    David A. Black
     
    David A. Black, Apr 4, 2005
    #2
    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. Replies:
    1
    Views:
    346
    fortepianissimo
    Jan 21, 2005
  2. grocery_stocker
    Replies:
    9
    Views:
    768
    grocery_stocker
    May 24, 2008
  3. gga
    Replies:
    5
    Views:
    114
  4. Nikita Petrov
    Replies:
    2
    Views:
    114
    Gary Wright
    Apr 6, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    325
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page