# Check my class

**URL:** <https://www.ruby-forum.com/t/check-my-class/226016>\
**Category:** Ruby\
**Created:** [July 16, 2008, 5:51am UTC](https://www.ruby-forum.com/t/check-my-class/226016 "2008-07-16T05:51:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 16, 2008, 5:51am UTC](https://www.ruby-forum.com/t/check-my-class/226016/1 "2008-07-16T05:51:44Z")

</div>

Hey i’m super new at ruby, This is my first class attempt. can i copy  
and paste this into the prompt? or do i need to save it as a .rb file?  
or how do i load it?  
help? haha

class Item  
def initialize(item,price,cost)  
@price = price  
@item = item  
@cost = cost  
@tax = 0.06  
@sales = 0  
@salesCount = 0  
@salesTaxPayable  
@remainingCost = cost  
@revPer = @price-@taxPer  
@taxPer = @price\*@tax  
@cash = 0  
end  
def sale(discount\_price)  
if discount\_price == 0 then  
@salesTaxPayable = @salesTaxPayable + @taxPer  
@sales = @sales + @revPer  
@cash = @cash + @price  
@remainingCost = @remainingCost - @revPer  
@saleCount = @saleCount + 1  
else  
@salesTaxPayable = @salesTaxPayable + @taxPer  
@sales = @sales + (discount\_price - (discount\_price\*@tax))  
@cash = @cash + discount\_price  
@remainingCost = @remainingCost - (discount\_price -  
(discount\_price\*@tax))  
@saleCount = @saleCount + 1  
end  
end  
def cash  
return @cash  
end  
def salesTax  
return @salesTaxPayable  
end  
end

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 16, 2008, 6:26am UTC](https://www.ruby-forum.com/t/check-my-class/226016/2 "2008-07-16T06:26:51Z")

</div>

> 

attr\_reader :cash, :salesTaxPayable  
alias :salesTax salesTaxPayable

Jayanth

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 16, 2008, 6:14am UTC](https://www.ruby-forum.com/t/check-my-class/226016/3 "2008-07-16T06:14:34Z")

</div>

On Wed, 2008-07-16 at 12:45 +0900, removed\_email\_address@domain.invalid wrote:

> Hey i’m super new at ruby, This is my first class attempt. can i copy  
> and paste this into the prompt? or do i need to save it as a .rb file?

Just as a general guideline for the future, if you have a question like  
“can I do ?” it might be best to just try it first. For example:

$ irb  
irb(main):001:0\> class Item  
irb(main):002:1\> def initialize(item,price,cost)  
irb(main):003:2\> @price = price  
irb(main):004:2\> @item = item  
irb(main):005:2\> @cost = cost  
irb(main):006:2\> @tax = 0.06  
irb(main):007:2\> @sales = 0  
irb(main):008:2\> @salesCount = 0  
irb(main):009:2\> @salesTaxPayable  
irb(main):010:2\> @remainingCost = cost  
irb(main):011:2\> @revPer = @price-@taxPer  
irb(main):012:2\> @taxPer = @price\*@tax  
irb(main):013:2\> @cash = 0  
irb(main):014:2\> end  
irb(main):015:1\> def sale(discount\_price)  
irb(main):016:2\> if discount\_price == 0 then  
irb(main):017:3\* @salesTaxPayable =  
@salesTaxPayable + @taxPer  
irb(main):018:3\> @sales = @sales + @revPer  
irb(main):019:3\> @cash = @cash + @price  
irb(main):020:3\> @remainingCost = @remainingCost

- @revPer  
irb(main):021:3\> @saleCount = @saleCount + 1  
irb(main):022:3\> else  
irb(main):023:3\* @salesTaxPayable =  
@salesTaxPayable + @taxPer  
irb(main):024:3\> @sales = @sales +  
(discount\_price - (discount\_price\*@tax))  
irb(main):025:3\> @cash = @cash + discount\_price  
irb(main):026:3\> @remainingCost = @remainingCost
- (discount\_price -  
irb(main):027:4\* (discount\_price\*@tax))  
irb(main):028:3\> @saleCount = @saleCount + 1  
irb(main):029:3\> end  
irb(main):030:2\> end  
irb(main):031:1\> def cash  
irb(main):032:2\> return @cash  
irb(main):033:2\> end  
irb(main):034:1\> def salesTax  
irb(main):035:2\> return @salesTaxPayable  
irb(main):036:2\> end  
irb(main):037:1\> end  
=\> nil  
irb(main):038:0\>

It sure looks to me like you can just cut and paste it into irb. 😉 It  
took me literally ten seconds (I had to wait for my terminal to load  
because my memory is topped) to check this.

You’ll find that Ruby is very forgiving of this kind of experimentation.  
If you can do it, it will let you do it. If you can’t, it will tell you  
you can’t without trashing your system.

Now, on to your class:

> @revPer = @price-@taxPer  
> @taxPer = @price\*@tax  
> @cash = 0  
> end

If you want to be more concise, try:

```
    @price, @item, @cost = [price, item, cost]
    @sales = @salesCount = @remainingCost = @cash = 0

```

I don’t know what you want to do with @salesTaxPayable because as it  
stands nothing happens. You also pre-calculate some things which may be  
premature. Why not calculate at need and cache?

> def sale(discount\_price)  
> if discount\_price == 0 then  
> @salesTaxPayable = @salesTaxPayable + @taxPer

```
    @salesTaxPayable += @taxPer

```

In general anytime you find yourself doing the “x = x y”  
thing, try “x = y” instead.

> ```
> @saleCount = @saleCount + 1
> end
> 
> ```
> 
> end

In this function you’re duplicating a lot of code. There’s code in the  
if and in the else blocks which are identical. Take them out and  
calculate them in one place:

```
    def sale(discount_price = 0)

        @salesTaxPayabe += @taxPer

        if discount_price == 0 then
            @sales = @sales + @revPer
            @cash = @cash + @price
            @remainingCost = @remainingCost - @revPer
        else
            @sales = @sales + (discount_price - 

```

(discount\_price\*@tax))  
@cash = @cash + discount\_price  
@remainingCost = @remainingCost - (discount\_price -  
(discount\_price\*@tax))  
end

```
        @saleCount += 1

    end

```

You’ll note that I’ve taken the liberty of giving the discount a default  
value of 0. This is just a personal style choice, but I prefer to have  
the most common circumstances be the defaults in my code. I’ve moved  
the common calculations out of the branch, putting the sale count  
increment at the end and the sales tax payable at the beginning. The  
count can be put anywhere, but at the end makes the most sense for my  
reading.

> def cash  
> return @cash  
> end  
> def salesTax  
> return @salesTaxPayable  
> end

Try this instead:

```
    attr_reader :cash, :salesTax

```

While you’re at it, look up attr\_writer and attr\_accessor as well. Ruby  
does a lot to help you avoid the creation of boilerplate code.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 16, 2008, 6:55am UTC](https://www.ruby-forum.com/t/check-my-class/226016/4 "2008-07-16T06:55:30Z")

</div>

On Wed, 2008-07-16 at 13:18 +0900, Srijayanth S. wrote:

> > 
> 
> attr\_reader :cash, :salesTaxPayable  
> alias :salesTax salesTaxPayable

Doh! Good catch. I didn’t see that the accessor was differently named  
from the member.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 16, 2008, 10:28am UTC](https://www.ruby-forum.com/t/check-my-class/226016/5 "2008-07-16T10:28:33Z")

</div>

On 16 Jul 2008, at 05:35, removed\_email\_address@domain.invalid wrote:

> > you can’t without trashing your system.
> 
> I’m on a mac, and I used textedit to write the class, then copied and  
> pasted but after ever def and many other lines after that it asked me  
> Display all 319 possibilities? (y or n)  
> so thats why i was curious if i should be able to or not…would you  
> know why it does that?  
> Because your file probably used tabs (the convention usually to use  
> soft tabs instead, 2 spaces per tab).  
> In an interactive session with irb tab means auto complete so pasting  
> a document with tabs in it won’t end well.  
> Don’t know if you can set textedit to use spaces instead of tabs.

Fred

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 16, 2008, 6:42am UTC](https://www.ruby-forum.com/t/check-my-class/226016/6 "2008-07-16T06:42:02Z")

</div>

> It sure looks to me like you can just cut and paste it into irb. 😉 It  
> took me literally ten seconds (I had to wait for my terminal to load  
> because my memory is topped) to check this.
> 
> You’ll find that Ruby is very forgiving of this kind of experimentation.  
> If you can do it, it will let you do it. If you can’t, it will tell you  
> you can’t without trashing your system.

I’m on a mac, and I used textedit to write the class, then copied and  
pasted but after ever def and many other lines after that it asked me  
Display all 319 possibilities? (y or n)  
so thats why i was curious if i should be able to or not…would you  
know why it does that?

as for the help on the class thank you! i am coming from php and i  
started looking into ruby yesterday…so i’m still unsure of what is  
the same or different or what is allowed…But thank you.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 16, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/check-my-class/226016/7 "2008-07-16T15:40:41Z")

</div>

On Jul 16, 2:23 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Because your file probably used tabs (the convention usually to use  
> soft tabs instead, 2 spaces per tab).  
> In an interactive session with irb tab means auto complete so pasting  
> a document with tabs in it won’t end well.  
> Don’t know if you can set textedit to use spaces instead of tabs.
> 
> Fred

Perfect fix. thanks

Here’s the my first functioning class.

class Item  
attr\_reader :cash, :salesTaxPayable, :salesCount, :sales  
def initialize(item,price,cost)  
@item, @price, @cost = [item, price, cost]  
@sales = @salesCount = @cash = @salesTaxPayable = 0  
@tax = 0.06  
@unCoveredCost = cost  
end  
def sale(discount\_price = false, qty = 1)  
if discount\_price == false then  
@salesTaxPayable += ((@price_qty) \* @tax)  
 @sales += (@price - ((@price_qty)_@tax))  
 @cash += @price_qty  
@unCoveredCost -= ((@price_qty) - ((@price_qty)_@tax))  
else  
 @salesTaxPayable += (@price_@tax)  
@sales = @sales + ((discount\_price_qty) -  
((discount\_price_qty)_@tax))  
 @cash = @cash + (discount\_price_qty)  
@remainingCost = @unCoveredCost - ((discount\_price_qty) -  
((discount\_price_qty)\*@tax))  
end  
@salesCount += 1  
end  
end

(changed all the tabs to spaces)

Hurray. And it worked for what i needed. temporary cash register haha  
Tell me what you think about making a variable for the repeated  
calculations…Michael said it was better to just do the calcs as they  
are needed? so i did. i’m trying to learn the “ruby way” of making  
things simpler and more elegant
