Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

ruby problem

Name: Anonymous 2012-10-05 14:43

I have a ruby problem. I have to read a file which contains the name of an item and the price without VAT. So far I have 2 classes shop and item and a main script.

The main reads the data in the file and creates an item datatype and pushes it into an array.

The item class has 2 variables name and price_without_VAT. It also has a method real_price which calculates the price with VAT added.


VAT = 0.2
class Item
  attr_accessor :name,:price

  def initialize name,price
    @name = name
    @price = price
  end

  def real_price
    price_with_vat =  (@price + (@price * VAT))
    return price_with_vat
  end
end


shop class

class Shop
  def print (itemsArray)#print all items in shop, and post VAT price
    itemsArray.length.times do |i|
      puts itemsArray.at(i).name + " " + itemsArray.at(i).real_price.to_s
    end
  end

  def total_value(itemsArray) #return total value of items
    total = 0
    itemsArray.length.times do |i|
      total = total + itemsArray.at(i).real_price.to_f
    end
    return total
  end

  def average_price(itemsArray) #return average price incl VAT of items in show
    average = total_value(itemsArray)/itemsArray.length
    return average
  end
end


In my main script I read the data from file using a loop, into an Item and push it into array.


I want to pass specific array entry and be able to call real_price to calculate price with VAT.

How do? I've tried itemsArray.at(i).real_price but it just prints out item price without vat. also would it be best to put main script in either of the classes like shop or item class

Name: Anonymous 2012-10-05 14:47

I have a ruby problem
ruby
/g/

Name: Anonymous 2012-10-05 15:48

ruby
Now you have two problems.

Name: Anonymous 2012-10-05 16:02

ruby is an acronym for R U Bisexual Yet?

Name: Anonymous 2012-10-05 16:08

< Andys> oh dear
< Andys> in ruby, symbols are represented with a prepended colon
< Andys> eg.   :flag
< Andys> so some guy tshirt that said ":sex"
< Andys> which everyone at railscamp knew meant "Sex symbol"
< Andys> he wore it until someone pointed out that to non-rubyists it said "Colon sex"

Name: Anonymous 2012-10-05 16:54

Voluntarily coding in ruby? Please end your own life.

Name: Anonymous 2012-10-05 17:11

>>1                        `
>OOP


ISHYGDDT

Name: Anonymous 2012-10-05 17:27

>>7
back to /g/ shitstain

Don't change these.
Name: Email:
Entire Thread Thread List