private method - Fixnum (NoMethodError)

Discussion in 'Ruby' started by salai, Jun 17, 2009.

  1. salai

    salai Guest

    Dear All,

    I can't find the error in my code. Can you explain me "private method
    - Fixnum (NoMethodError)"?

    Many thanks in advance,

    koko

    def subtotal(qty = 1)
    return nil if self.to_f <= 0 || qty.to_f <= 0
    self.to_f * qty.to_f
    end

    apple = 11

    puts apple.subtotal(2)
     
    salai, Jun 17, 2009
    #1
    1. Advertising

  2. Hi,

    On Wed, Jun 17, 2009 at 9:13 AM, salai<> wrote:
    > I can't find the error in my code. Can you explain me "private method
    > - =C2=A0Fixnum (NoMethodError)"?
    >
    > def subtotal(qty =3D 1)
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0return nil if self.to_f <=3D 0 || qty.to_f <=

    =3D 0
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.to_f * qty.to_f
    > end
    >
    > apple =3D 11
    >
    > puts apple.subtotal(2)


    You've defined the 'subtotal' method in the global scope, which is
    equivalent to defining a private instance method on Object. Private
    methods can't be called with a receiver. If you really want to do this
    (this method seems bizarre to me), you'll want to reopen one or more
    of the numeric classes and define your method there, e.g.,

    class Fixnum
    def subtotal qty =3D 1
    return nil if to_f <=3D 0 || qty.to_f <=3D 0
    to_f * qty.to_f
    end
    end

    apple =3D 11

    puts apple.subtotal(2)
    # 22.0


    ~ j.
     
    John Barnette, Jun 17, 2009
    #2
    1. Advertising

  3. On Wednesday 17 June 2009, salai wrote:
    > |Dear All,
    > |
    > |I can't find the error in my code. Can you explain me "private method
    > |- Fixnum (NoMethodError)"?
    > |
    > |Many thanks in advance,
    > |
    > |koko
    > |
    > |def subtotal(qty = 1)
    > | return nil if self.to_f <= 0 || qty.to_f <= 0
    > | self.to_f * qty.to_f
    > |end
    > |
    > |apple = 11
    > |
    > |puts apple.subtotal(2)


    First of all, when you ask about an error, it's better to report the whole
    error message, not just a bit of it. At any rate, running your code I get the
    error message:

    private method `subtotal' called for 11:Fixnum (NoMethodError)

    this means that you're calling a private method in a forbidden way, that is
    using the object.method_name notation. Private methods can only be called on
    the explicit receiver (self). Now you may be asking, "why subtotal is a
    private method?" The answer is that methods defined toplevel (that is, methods
    which aren't defined inside a module or a class and aren't singleton methods)
    automatically become private methods of class Object, so you can't call them
    with an explicit receiver.

    You have at least two options to fix your code, depending on what exactly you
    need. You can:
    1) define your subtotal method like this:
    def subtotal( arg1, qty=1)
    return nil if arg1.to_f <= 0 || qty.to_f <= 0
    arg1.to_f * qty.to_f
    end

    and transform apple from the explicit receiver of the call to the first
    argument to subtotal:

    puts subtotal(apple, 2)

    2) define the subtotal method in a class. For example, you can define it for
    the Fixnum class:

    class Fixnum
    def subtotal(qty = 1)
    return nil if self.to_f <= 0 || qty.to_f <= 0
    self.to_f * qty.to_f
    end
    end

    then you can call it as you originally did:
    puts apple.subtotal(2)

    I hope this helps

    Stefano
     
    Stefano Crocco, Jun 17, 2009
    #3
  4. salai

    salai Guest

    Thanks you very much to you all.


    regards,
    salai.

    On Wed, Jun 17, 2009 at 6:33 PM, Stefano Crocco<> wr=
    ote:
    > On Wednesday 17 June 2009, salai wrote:
    >> |Dear All,
    >> |
    >> |I can't find the error in my code. Can you explain me "private method
    >> |- =A0Fixnum (NoMethodError)"?
    >> |
    >> |Many thanks in advance,
    >> |
    >> |koko
    >> |
    >> |def subtotal(qty =3D 1)
    >> | =A0 =A0 return nil if self.to_f <=3D 0 || qty.to_f <=3D 0
    >> | =A0 =A0 self.to_f * qty.to_f
    >> |end
    >> |
    >> |apple =3D 11
    >> |
    >> |puts apple.subtotal(2)

    >
    > First of all, when you ask about an error, it's better to report the whol=

    e
    > error message, not just a bit of it. At any rate, running your code I get=

    the
    > error message:
    >
    > private method `subtotal' called for 11:Fixnum (NoMethodError)
    >
    > this means that you're calling a private method in a forbidden way, that =

    is
    > using the object.method_name notation. Private methods can only be called=

    on
    > the explicit receiver (self). Now you may be asking, "why subtotal is a
    > private method?" The answer is that methods defined toplevel (that is, me=

    thods
    > which aren't defined inside a module or a class and aren't singleton meth=

    ods)
    > automatically become private methods of class Object, so you can't call t=

    hem
    > with an explicit receiver.
    >
    > You have at least two options to fix your code, depending on what exactly=

    you
    > need. You can:
    > 1) define your subtotal method like this:
    > =A0def subtotal( arg1, qty=3D1)
    > =A0 =A0return nil if arg1.to_f <=3D 0 || qty.to_f <=3D 0
    > =A0 =A0arg1.to_f * qty.to_f
    > =A0end
    >
    > and transform apple from the explicit receiver of the call to the first
    > argument to subtotal:
    >
    > =A0puts subtotal(apple, 2)
    >
    > 2) define the subtotal method in a class. For example, you can define it =

    for
    > the Fixnum class:
    >
    > =A0class Fixnum
    > =A0 =A0def subtotal(qty =3D 1)
    > =A0 =A0 =A0return nil if self.to_f <=3D 0 || qty.to_f <=3D 0
    > =A0 =A0 =A0 self.to_f * qty.to_f
    > =A0 =A0end
    > =A0end
    >
    > =A0then you can call it as you originally did:
    > =A0puts apple.subtotal(2)
    >
    > I hope this helps
    >
    > Stefano
    >
    >
     
    salai, Jun 30, 2009
    #4
    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. anon1m0us
    Replies:
    2
    Views:
    225
    anon1m0us
    Feb 6, 2007
  2. Sukeerthi Adiga

    NoMethodError: private method `to_date'

    Sukeerthi Adiga, Feb 25, 2008, in forum: Ruby
    Replies:
    11
    Views:
    417
    P. mark Anderson
    Jul 24, 2008
  3. Heesob Park

    Why Fixnum===Fixnum is false?

    Heesob Park, May 13, 2009, in forum: Ruby
    Replies:
    5
    Views:
    133
    Joel VanderWerf
    May 14, 2009
  4. Joe dude
    Replies:
    1
    Views:
    167
    Ryan Davis
    Nov 30, 2009
  5. MaggotChild
    Replies:
    6
    Views:
    148
    MaggotChild
    Dec 2, 2009
Loading...

Share This Page