From: Axel Etzold Date: 2008-06-10T21:07:52+09:00 Subject: Re: C++ 2 Ruby -------- Original-Nachricht -------- > Datum: Tue, 10 Jun 2008 19:43:40 +0900 > Von: Jed Kowalski > An: ruby-talk@ruby-lang.org > Betreff: Re: C++ 2 Ruby > Axel Etzold wrote: > > Jed, > > > > if you ask for a variable using 'gets', this gives you a String. > > So you have to write > > > > mystring=gets > > case mystring > > when '1' # instead of when 1 > > # .... > > > > I would love to be able to speak Polish ... it's amazing that people who > > can still ask questions > > about anything :) > > > > Best regards, > > > > Axel > > THX!!! > and now, the last (i think) problem: > the change(x) method doesn't want to work, can You tell me why? > > BTW can somebody check for other errors? > > class Drink > > attr_accessor :price,:ammount,:name,:bought > > def initialize (cen, ile, nn) > $price=cen > $ammount=ile > $name=nn > $bought=0 > end > > def chage(x) > $ammount-=1 > $bought+=1 > return x - self.pricea > end > def ammounti() return $ammount end > def pricea() return $price end > def buy() return $bought end > def canbuy() > if $ammount==0 then > return false > else return true > end > end > def namea() puts $name end > > end > class Automat > > def initialize (pie, dwa, jed, piec, dwad, dzi) > $tab = [[pie,dwa, jed, piec, dwad, dzi],[5,2,1,0.5,0.2,0.1]] > puts $tab > > > end > def coins(n, s, g) > x=s-n[g].pricea > for i in 0.. 5 > w=$tab[0][i] > for w in w..0 > if (x-$tab[1][i])> -0.0001 then > $tab[0][i]=$tab[0][i]-1 > x=x-$tab[1][i] > puts $tab[1][i] > end > end > end > end > def write() > puts "Coins 5zl: ", $tab[0][0] > puts "Coins 2zl: ", $tab[0][1] > puts "Coins 1zl: ", $tab[0][2] > puts "Coins 50gr: ", $tab[0][3] > puts "Coins 20gr: ", $tab[0][4] > puts "Coins 10gr: ", $tab[0][5] > end > def dodaj(x) > if x==5 then $tab[0][0]=$tab[0][0]+1 end > if x==2 then $tab[0][1]=$tab[0][1]+1 end > if x==1 then $tab[0][2]=$tab[0][2]+1 end > if x==0.5 then $tab[0][3]=$tab[0][3]+1 end > if x==0.2 then $tab[0][4]=$tab[0][4]+1 end > if x==0.1 then $tab[0][5]=$tab[0][5]+1 end > end > end > > > > q=1 > m=1 > s=0 > p = Automat.new(15,15,20,25,25,25) > drinks = [ > Drink.new(1.7,0,"Coca-Cola"), > Drink.new(2,0,"Sprite"), > Drink.new(2.1,20,"Fanta") ] > > > while q!=0 > puts "What do you want to do?" > puts "Buy Drink: 1" > puts "Coin report: 2" > puts "Drink report: 3" > puts "Exit: 0" > > q = gets.strip.to_i > puts q > puts q > > case q > > when 1: > > for j in 0..2 > drinks[j].namea() > puts ": ",drinks[j].pricea()," press ",j > end > g = gets.strip.to_i > if drinks[g].canbuy() then > y=1 > s=0 > while y!=2 > puts "Coins inserted: ",s," What to do: \n\t\t Input > coins: 0 \n\t\t Buy Drink: 1 \n\t\t Cancel: 2" > puts "Your choice: " > y = gets.strip.to_i > if y==0 then > puts "Input coins. " > m = gets.strip.to_f > s=s+m > p.dodaj(m) > end > if y==1 && drinks[g].chage(s)<0 then puts "No chash. Iput coins" > else > puts "reszta: " > p.coins(drinks,s,g) > end > #if y==2 then break end > end > > > else puts "No such Drinku." > end > when 2: > p.write() > when 3: > puts "You boughts: " > for i in 0..2 > drinks[i].namea() > puts ": ", drinks[i].buy() > end > end > end > -- > Posted via http://www.ruby-forum.com/. Jed, you don't have an accessor for the instance variable pricea . It's better not to use global variables (starting with $), also, but instance variables (starting with @): class Drink :attr_accessor :pricea, #... def initialize (cen, ile, nn) @price=cen @ammount=ile @name=nn @bought=0 end end If you define a function 'pricea()', then you need to call it as obj.pricea() also, not as obj.pricea ... but in Ruby, people generally don't use the first form. Concerning usage of classes, you might find this enlightening: http://pine.fm/LearnToProgram/?Chapter=09 (and of course, the whole tutorial, not just chapter 9). Best regards, Axel -- GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen! Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx