Calling a method with an instance

Hi. I'm playing around with creating methods and then creating an
instance that calls the instance. Here's my code.....

class Discount
attr_accessor :amount, :discount

def initialize(am, dis)
@amount = am
@discount = dis

end

def self.discount_amount(amt, disc)
newamount = amt - disc
end

end

dis1 = Discount.new(100, 20)

dis1.discount_amount(amount, discount)

The error I get is......

discount.rb:25:in `<main>': undefined local variable or method `amount'
for main
:Object (NameError)

What is the best way to call a method with an instance?
Thanks

···

--
Posted via http://www.ruby-forum.com/.

I think u want:
dis1.discount_amount(dis1.amount, discount)

···

On Oct 5, 2:59 pm, Paul Roche <prpaulro...@gmail.com> wrote:

Hi. I'm playing around with creating methods and then creating an
instance that calls the instance. Here's my code.....

class Discount
attr_accessor :amount, :discount

def initialize(am, dis)
@amount = am
@discount = dis

end

def self.discount_amount(amt, disc)
newamount = amt - disc
end

end

dis1 = Discount.new(100, 20)

dis1.discount_amount(amount, discount)

The error I get is......

discount.rb:25:in `<main>': undefined local variable or method `amount'
for main
:Object (NameError)

What is the best way to call a method with an instance?
Thanks
--
Posted viahttp://www.ruby-forum.com/.

There are a couple issues. First of all, you are trying to use two
variables (amount and discount) without giving them any values, in other
words without defining them. That's why you get the error "undefined
local variable or method `amount'". Perhaps you mean to use dis1.amount
and dis1.discount instead.

Once you clear that hurdle, you'll find you also declared the
discount_amount method as a class method on Discount, not an instance
method. However, you go on to call the method as an instance method of
dis1. I think the corrected code would be as follows:

Discount.discount_amount(dis1.amount, dis1.discount)

-Jeremy

···

On 10/5/2010 1:59 PM, Paul Roche wrote:

Hi. I'm playing around with creating methods and then creating an
instance that calls the instance. Here's my code.....

class Discount
attr_accessor :amount, :discount

def initialize(am, dis)
@amount = am
@discount = dis

end

def self.discount_amount(amt, disc)
newamount = amt - disc
end

end

dis1 = Discount.new(100, 20)

dis1.discount_amount(amount, discount)

The error I get is......

discount.rb:25:in `<main>': undefined local variable or method `amount'
for main
:Object (NameError)

What is the best way to call a method with an instance?
Thanks

this works, but not sure if this approach is most sensible

dis1.discount_amount(dis1.amount, dis1.discount)

···

On Oct 5, 3:20 pm, Jedrin <jrubia...@gmail.com> wrote:

On Oct 5, 2:59 pm, Paul Roche <prpaulro...@gmail.com> wrote:

> Hi. I'm playing around with creating methods and then creating an
> instance that calls the instance. Here's my code.....

> class Discount
> attr_accessor :amount, :discount

> def initialize(am, dis)
> @amount = am
> @discount = dis

> end

> def self.discount_amount(amt, disc)
> newamount = amt - disc
> end

> end

> dis1 = Discount.new(100, 20)

> dis1.discount_amount(amount, discount)

> The error I get is......

> discount.rb:25:in `<main>': undefined local variable or method `amount'
> for main
> :Object (NameError)

> What is the best way to call a method with an instance?
> Thanks
> --
> Posted viahttp://www.ruby-forum.com/.

I think u want:
dis1.discount_amount(dis1.amount, discount)

Jedrin wrote:

···

On Oct 5, 3:20�pm, Jedrin <jrubia...@gmail.com> wrote:

> �def initialize(am, dis)

> What is the best way to call a method with an instance?
> Thanks
> --
> Posted viahttp://www.ruby-forum.com/.

I think u want:
dis1.discount_amount(dis1.amount, discount)

this works, but not sure if this approach is most sensible

dis1.discount_amount(dis1.amount, dis1.discount)

Thanks for this. What is the best way to go about dealing with a method
such as this that has two parameters?
--
Posted via http://www.ruby-forum.com/\.

Um, I think you guys mean
Discount.discount_amount(dis1.amount,dis1.discount)

discount_amount is not an instance method. Or am I missing something?

···

On Tue, Oct 5, 2010 at 3:25 PM, Jedrin <jrubiando@gmail.com> wrote:

On Oct 5, 3:20 pm, Jedrin <jrubia...@gmail.com> wrote:
> On Oct 5, 2:59 pm, Paul Roche <prpaulro...@gmail.com> wrote:
>
>
>
> > Hi. I'm playing around with creating methods and then creating an
> > instance that calls the instance. Here's my code.....
>
> > class Discount
> > attr_accessor :amount, :discount
>
> > def initialize(am, dis)
> > @amount = am
> > @discount = dis
>
> > end
>
> > def self.discount_amount(amt, disc)
> > newamount = amt - disc
> > end
>
> > end
>
> > dis1 = Discount.new(100, 20)
>
> > dis1.discount_amount(amount, discount)
>
> > The error I get is......
>
> > discount.rb:25:in `<main>': undefined local variable or method `amount'
> > for main
> > :Object (NameError)
>
> > What is the best way to call a method with an instance?
> > Thanks
> > --
> > Posted viahttp://www.ruby-forum.com/.
>
> I think u want:
> dis1.discount_amount(dis1.amount, discount)

this works, but not sure if this approach is most sensible

dis1.discount_amount(dis1.amount, dis1.discount)

I think the comment about sensibility regards the passing of an object's
internal values to one of the object's own methods. Assuming you
redefine the discount_amount method as an instance method, there is no
need to pass in these values since they will be available to the method
as @amount and @discount:

class Discount
  attr_accessor :amount, :discount

  def initialize(amount, discount)
    @amount = amount
    @discount = discount
  end

  # defined discount_amount as an instance method rather than a
  # class method
  def discount_amount
    @amount - @discount
  end
end

dis1 = Discount.new(100, 20)
dis1.discount_amount # => 80

-Jeremy

···

On 10/5/2010 2:32 PM, Paul Roche wrote:

Jedrin wrote:

On Oct 5, 3:20�pm, Jedrin <jrubia...@gmail.com> wrote:

�def initialize(am, dis)

What is the best way to call a method with an instance?
Thanks
--
Posted viahttp://www.ruby-forum.com/.

I think u want:
dis1.discount_amount(dis1.amount, discount)

this works, but not sure if this approach is most sensible

dis1.discount_amount(dis1.amount, dis1.discount)

Thanks for this. What is the best way to go about dealing with a method
such as this that has two parameters?