[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

C++ 2 Ruby

Jed Kowalski

6/9/2008 6:41:00 PM

Greetings Ruby fans :)
From a couple of months I've been learning C++ but after I found out
about Ruby... it's just GREAT!
Right now im at a beginner level, trying to convert some simplier code
to Ruby (it's easier for me to learn this way)
Can somebody please help me a little? ;>
Ofcourse a working example would be also nice ;)

BTW sorry for my English...

//---------------------------------------------------------------------------
#include<math.h> //WHAT TO DO WITH THOSE
#include<iostream>
#include<cstdio>

//---------------------------------------------------------------------------
#pragma argsused
using namespace std;
class Drink{
double price;
char name[20];
int amount,bought;
public:
Drink(double cen, int how_much, char* nn) {
price=cen;
amount=how_much;
strcpy(name,nn);
bought=0;
};
double change(double x){
amount--; //HOW TO DO THIS
bought++;
return x-price;
};
int amounti() {return amount;};
double pricea(){return price;};
int buy(){return bought;};
bool canbuy(){
if (amount==0)
return false;
else return true;
};
void namea(){cout<<name;};
friend class Automat; //WHAT TO DO WIGHT THIS
};
class Automat{
private:
double tab[2][8],u,w,x;
public:
Automat(double pie,double dwa,double jed,double piec,double
dwad,double dzi)
{
tab[0][0]=pie;
tab[0][1]=dwa;
tab[0][2]=jed;
tab[0][3]=piec;
tab[0][4]=dwad;
tab[0][5]=dzi;
tab[1][0]=5;
tab[1][1]=2;
tab[1][2]=1;
tab[1][3]=0.5;
tab[1][4]=0.2;
tab[1][5]=0.1;
};
void Coinsy(Drink *n,double s,int g){
x=s-n[g].price;
for (int i = 0; i <=5; i++) {
w=tab[0][i];
for (double j =w; j>0; j--) {
if ((x-tab[1][i])> -0.0001) {
tab[0][i]--;
x=x-tab[1][i];
cout<<tab[1][i]<<endl;
}
}
}
};
void write(){
cout<<"Coins 5zl: "<<tab[0][0]<<endl;
cout<<"Coins 2zl: "<<tab[0][1]<<endl;
cout<<"Coins 1zl: "<<tab[0][2]<<endl;
cout<<"Coins 50gr: "<<tab[0][3]<<endl;
cout<<"Coins 20gr: "<<tab[0][4]<<endl;
cout<<"Coins 10gr: "<<tab[0][5]<<endl;
};
void add(double x){
if (x==5) tab[0][0]++;
if (x==2) tab[0][1]++;
if (x==1) tab[0][2]++;
if (x==0.5)tab[0][3]++;
if (x==0.2)tab[0][4]++;
if (x==0.1)tab[0][5]++;
}
};

int main()
{
int q=1,y,i,g;
double m=1,s=0;
Automat p(15,15,20,25,25,25);
Drink Drinke[3]= {
Drink(1.7,0,"Coca-Cola"),
Drink(2,0,"Sprite"),
Drink(2.1,20,"Fanta")
};

while(q!=0)
{
cout<<"What would you like to do?"<<endl;
cout<<"Kupic Drink: press 1"<<endl;
cout<<"Print Coins report: press 2"<<endl;
cout<<"Print bought Drinks report: press 3"<<endl;
cout<<"Exit: press 0"<<endl;
cin>>q;
switch (q) //I'M NOT QUITE SURE HOW TO USE SWITCH IN RUBY
{
case 1: {
system("cls");
for (int j = 0; j <3; j++) {
Drinke[j].namea();
cout<<": "<<Drinke[j].pricea()<<" nacisnij "<<j<<endl;
}
cin>>g;
if (Drinke[g].canbuy()) {
y=1;
s=0;
while (y!=0){
cout<<"Coins Input: "<<s<<endl<<" What to do: \n\t\t
Input Coins: 0 \n\t\t Buy Drink: 1 \n\t\t Cancel: 2"<<endl;
cout<<"Your choice: ";
cin>>y;
if (y==0) {
cout<<"Input Coins. ";
cin>>m;
s+=m;
p.add(m);
}
if ((y==1)&&(Drinke[g].change(s)<0)) {cout<<"No cash. Input
Coins."<<endl;}
else {
cout<<"change: "<<endl;
p.Coinsy(Drinke,s,g);
}
if (y==2) { break; }
}
}
else cout<<"No such drink."<<endl;
}
case 2:
p.write();
case 3: {
system("cls"); //CAN YOU CLEAR SCREEN IN RUBY?
cout<<"Drinks bought: "<<endl;
for (int i=0; i <3; i++) {
Drinke[i].namea();
cout<<": "<<Drinke[i].buy()<<endl;
}
cout<<endl;
break;
}
}
}
getchar(); getchar();
return 0;
}
//---------------------------------------------------------------------------
--
Posted via http://www.ruby-....

12 Answers

Avdi Grimm

6/9/2008 7:14:00 PM

0

On Mon, Jun 9, 2008 at 2:40 PM, Jed Kowalski <satanowicz@gmail.com> wrote:
> Greetings Ruby fans :)
> From a couple of months I've been learning C++ but after I found out
> about Ruby... it's just GREAT!
> Right now im at a beginner level, trying to convert some simplier code
> to Ruby (it's easier for me to learn this way)
> Can somebody please help me a little? ;>
> Ofcourse a working example would be also nice ;)

You might have better luck asking for help with specific sections of
code, rather than asking us to rewrite a whole program. Try doing a
best-guest translation to Ruby on your own, and asking about the parts
you have trouble with.

--
Avdi

Home: http:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Jed Kowalski

6/9/2008 8:39:00 PM

0

Avdi Grimm wrote:
> You might have better luck asking for help with specific sections of
> code, rather than asking us to rewrite a whole program. Try doing a
> best-guest translation to Ruby on your own, and asking about the parts
> you have trouble with.

ok so, here's the first problem:
def initialize (cen, ile, nn)
@price=cen
@amount=ile
@name=nn
@bought=0
end


def change(x)
amount--
bought++
return x-price //WHATS WRONG HERE?
end

and I get:

gut.rb:13: void value expression
return x-cena
^
gut.rb:13: syntax error, unexpected tIDENTIFIER, expecting kEND
return x-cena

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

Stefan Krüger

6/9/2008 9:01:00 PM

0

Jed Kowalski wrote:
> def change(x)
> amount--
> bought++
> return x-price //WHATS WRONG HERE?
> end
>
> and I get:
>
> gut.rb:13: void value expression
> return x-cena


I think a google search for "Instance Variables" should enlighten you :)

Tim Hunter

6/9/2008 9:02:00 PM

0

Jed Kowalski wrote:
> Avdi Grimm wrote:
>> You might have better luck asking for help with specific sections of
>> code, rather than asking us to rewrite a whole program. Try doing a
>> best-guest translation to Ruby on your own, and asking about the parts
>> you have trouble with.
>
> ok so, here's the first problem:
> def initialize (cen, ile, nn)
> @price=cen
> @amount=ile
> @name=nn
> @bought=0
> end
>
>
> def change(x)
> amount--
> bought++
> return x-price //WHATS WRONG HERE?
> end
>
> and I get:
>
> gut.rb:13: void value expression
> return x-cena
> ^
> gut.rb:13: syntax error, unexpected tIDENTIFIER, expecting kEND
> return x-cena
>

Well, if you want to refer to the @amount, @bought, and @price instance
variables in the change() method you have to use the correct names. Also
Ruby doesn't have the -- and ++ methods.

def change(x)
@amount -= 1
@bought += 1
return x - @price
end





--
RMagick: http://rmagick.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

Axel Etzold

6/9/2008 9:14:00 PM

0


-------- Original-Nachricht --------
> Datum: Tue, 10 Jun 2008 05:38:30 +0900
> Von: Jed Kowalski <satanowicz@gmail.com>
> An: ruby-talk@ruby-lang.org
> Betreff: Re: C++ 2 Ruby

> Avdi Grimm wrote:
> > You might have better luck asking for help with specific sections of
> > code, rather than asking us to rewrite a whole program. Try doing a
> > best-guest translation to Ruby on your own, and asking about the parts
> > you have trouble with.
>
> ok so, here's the first problem:
> def initialize (cen, ile, nn)
> @price=cen
> @amount=ile
> @name=nn
> @bought=0
> end
>
>
> def change(x)
> amount--
> bought++
> return x-price //WHATS WRONG HERE?
> end
>
> and I get:
>
> gut.rb:13: void value expression
> return x-cena
> ^
> gut.rb:13: syntax error, unexpected tIDENTIFIER, expecting kEND
> return x-cena
>
> --
> Posted via http://www.ruby-....

Jed, you could do this:

class MyClass # my most creative invention of a class name
attr_accessor :price,:amount,:name,:bought # creates ways to get/set @price,@amount,... from MyClass

def initialize (cen, ile, nn)
@price=cen
@amount=ile
@name=nn
@bought=0
end
def change(x)
@amount-=1
@bought+=1
x-self.price
end
end

cen=500
ile=400
nn=4
obj=MyClass.new(cen,ile,nn)
p obj.change(1) # -499 =1-500

Read about accessors here: http://www.ruby-doc.org/docs/UsersGuide/rg/acce...

Best regards,

Axel
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/s...

Jed Kowalski

6/9/2008 10:53:00 PM

0

Axel Etzold wrote:
> Jed, you could do this:
>
> class MyClass # my most creative invention of a class name
> attr_accessor :price,:amount,:name,:bought # creates ways to get/set
> @price,@amount,... from MyClass
>
> def initialize (cen, ile, nn)
> @price=cen
> @amount=ile
> @name=nn
> @bought=0
> end
> def change(x)
> @amount-=1
> @bought+=1
> x-self.price
> end
> end
>
> cen=500
> ile=400
> nn=4
> obj=MyClass.new(cen,ile,nn)
> p obj.change(1) # -499 =1-500
>
> Read about accessors here:
> http://www.ruby-doc.org/docs/UsersGuide/rg/acce...
>
> Best regards,
>
> Axel

ufff... almoust done...
I can compile the code but it doesn't want to work, juz like the
variable q isn't seen properly, something with the 'case' instruction
maybe?
(BTW sorry for Polish texts)

class Napoj

def initialize (cen, ile, nn)
@cena=cen
@ilosc=ile
@nazwa=nn
@kupione=0
end

def wydajreszte(x)
@ilosc-=1
@kupione+=1
return x-cena
end
def ilosci() return ilosc end
def cenaa() return cena end
def kupno() return kupione end
def czyjest()
if ilosc==0 then
return false
else return true
end
end
def nazwaa() puts nazwa 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]]
end
def monety(n, s, g)
x=s-n[g].cena
for i in 1.. 5
w=tab[0][i]
for w in w..0
if (x-tab[1][i])> -0.0001 then
tab[0][i]-=1
x=x-tab[1][i]
puts tab[1][i]
end
end
end
end
def wypisz()
puts "Monet 5zl: ", tab[0][0]
puts "Monet 2zl: ", tab[0][1]
puts "Monet 1zl: ", tab[0][2]
puts "Monet 50gr: ", tab[0][3]
puts "Monet 20gr: ", tab[0][4]
puts "Monet 10gr: ", tab[0][5]
end
def dodaj(x)
if x==5 then tab[0][0]+=1 end
if x==2 then tab[0][1]+=1 end
if x==1 then tab[0][2]+=1 end
if x==0.5 then tab[0][3]+=1 end
if x==0.2 then tab[0][4]+=1 end
if x==0.1 then tab[0][5]+=1 end
end
end


q=1
m=1
s=0
p = Automat.new(15,15,20,25,25,25)
napoje = [
Napoj.new(1.7,0,"Coca-Cola"),
Napoj.new(2,0,"Sprite"),
Napoj.new(2.1,20,"Fanta") ]


while q!=0
puts "Co chcesz zrobic?"
puts "Kupic napoj: nacisnij 1"
puts "Wydrukowac raport o monetach: nacisnij 2"
puts "Wydrukowac raport o kupionych napojach: nacisnij 3"
puts "Wyjsc: nacisnij 0"

q = gets
puts q

case q

when 1

for j in 0..2
napoje[j].nazwaa()
puts ": "+napoje[j].cenaa()+" nacisnij "+j
end
g = gets
if napoje[g].czyjest() then
y=1
s=0
while y!=0
puts "Monety wrzucne: "+s+" Co chcesz zrobic: \n\t\t
Wrzuc monete: 0 \n\t\t Kup napoj: 1 \n\t\t Anuluj: 2"
puts "Twoj wybor: "
y = gets
if y==0 then
puts "Wrzuc monete. "
m = gets
s+=m
p.dodaj(m)
end
if y==1 && napoje[g].wydajreszte(s)<0 then puts "Brak srodkow.
Wrzuc monete."
else
puts "reszta: "
p.monety(napoje,s,g)
end
if y==2 then break end
end


else puts "Brak zadanego napoju."
end
when 2
p.wypisz()
when 3
puts "Kupiles: "
for i in 0..2
napoje[i].nazwaa()
puts ": ", napoje[i].kupno()
end
end
end
--
Posted via http://www.ruby-....

Axel Etzold

6/10/2008 7:33:00 AM

0


-------- Original-Nachricht --------
> Datum: Tue, 10 Jun 2008 07:53:25 +0900
> Von: Jed Kowalski <satanowicz@gmail.com>
> An: ruby-talk@ruby-lang.org
> Betreff: Re: C++ 2 Ruby

> Axel Etzold wrote:
> > Jed, you could do this:
> >
> > class MyClass # my most creative invention of a class name
> > attr_accessor :price,:amount,:name,:bought # creates ways to get/set
> > @price,@amount,... from MyClass
> >
> > def initialize (cen, ile, nn)
> > @price=cen
> > @amount=ile
> > @name=nn
> > @bought=0
> > end
> > def change(x)
> > @amount-=1
> > @bought+=1
> > x-self.price
> > end
> > end
> >
> > cen=500
> > ile=400
> > nn=4
> > obj=MyClass.new(cen,ile,nn)
> > p obj.change(1) # -499 =1-500
> >
> > Read about accessors here:
> > http://www.ruby-doc.org/docs/UsersGuide/rg/acce...
> >
> > Best regards,
> >
> > Axel
>
> ufff... almoust done...
> I can compile the code but it doesn't want to work, juz like the
> variable q isn't seen properly, something with the 'case' instruction
> maybe?
> (BTW sorry for Polish texts)
>
> class Napoj
>
> def initialize (cen, ile, nn)
> @cena=cen
> @ilosc=ile
> @nazwa=nn
> @kupione=0
> end
>
> def wydajreszte(x)
> @ilosc-=1
> @kupione+=1
> return x-cena
> end
> def ilosci() return ilosc end
> def cenaa() return cena end
> def kupno() return kupione end
> def czyjest()
> if ilosc==0 then
> return false
> else return true
> end
> end
> def nazwaa() puts nazwa 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]]
> end
> def monety(n, s, g)
> x=s-n[g].cena
> for i in 1.. 5
> w=tab[0][i]
> for w in w..0
> if (x-tab[1][i])> -0.0001 then
> tab[0][i]-=1
> x=x-tab[1][i]
> puts tab[1][i]
> end
> end
> end
> end
> def wypisz()
> puts "Monet 5zl: ", tab[0][0]
> puts "Monet 2zl: ", tab[0][1]
> puts "Monet 1zl: ", tab[0][2]
> puts "Monet 50gr: ", tab[0][3]
> puts "Monet 20gr: ", tab[0][4]
> puts "Monet 10gr: ", tab[0][5]
> end
> def dodaj(x)
> if x==5 then tab[0][0]+=1 end
> if x==2 then tab[0][1]+=1 end
> if x==1 then tab[0][2]+=1 end
> if x==0.5 then tab[0][3]+=1 end
> if x==0.2 then tab[0][4]+=1 end
> if x==0.1 then tab[0][5]+=1 end
> end
> end
>
>
> q=1
> m=1
> s=0
> p = Automat.new(15,15,20,25,25,25)
> napoje = [
> Napoj.new(1.7,0,"Coca-Cola"),
> Napoj.new(2,0,"Sprite"),
> Napoj.new(2.1,20,"Fanta") ]
>
>
> while q!=0
> puts "Co chcesz zrobic?"
> puts "Kupic napoj: nacisnij 1"
> puts "Wydrukowac raport o monetach: nacisnij 2"
> puts "Wydrukowac raport o kupionych napojach: nacisnij 3"
> puts "Wyjsc: nacisnij 0"
>
> q = gets
> puts q
>
> case q
>
> when 1
>
> for j in 0..2
> napoje[j].nazwaa()
> puts ": "+napoje[j].cenaa()+" nacisnij "+j
> end
> g = gets
> if napoje[g].czyjest() then
> y=1
> s=0
> while y!=0
> puts "Monety wrzucne: "+s+" Co chcesz zrobic: \n\t\t
> Wrzuc monete: 0 \n\t\t Kup napoj: 1 \n\t\t Anuluj: 2"
> puts "Twoj wybor: "
> y = gets
> if y==0 then
> puts "Wrzuc monete. "
> m = gets
> s+=m
> p.dodaj(m)
> end
> if y==1 && napoje[g].wydajreszte(s)<0 then puts "Brak srodkow.
> Wrzuc monete."
> else
> puts "reszta: "
> p.monety(napoje,s,g)
> end
> if y==2 then break end
> end
>
>
> else puts "Brak zadanego napoju."
> end
> when 2
> p.wypisz()
> when 3
> puts "Kupiles: "
> for i in 0..2
> napoje[i].nazwaa()
> puts ": ", napoje[i].kupno()
> end
> end
> end
> --
> Posted via http://www.ruby-....

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

--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/mult...

Jed Kowalski

6/10/2008 10:44:00 AM

0

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-....

Axel Etzold

6/10/2008 12:08:00 PM

0


-------- Original-Nachricht --------
> Datum: Tue, 10 Jun 2008 19:43:40 +0900
> Von: Jed Kowalski <satanowicz@gmail.com>
> 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-....


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/?...

(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_...

Jed Kowalski

6/10/2008 9:49:00 PM

0

how can I print a string and a variable in one line ?
i.e.

drinks[i].namea()
puts ": ", drinks[i].buy()


the output will be:

Sprite
:
0

and I would like to have:
Sprite: 0
--
Posted via http://www.ruby-....