Mam problem z tablicami w Ruby.
Chcę ten kod z c++ przenieść do Ruby
[code]#include <iostream.h>
using namespace std;
int main()
{
int nominal[6]={500,200,100,50,20,10};
int reszta;
cin>>a;
int n=0;
int ile[6]={0,0,0,0,0,0};
for ( ; ; )
{
if (a>=nominal[n])
{
ile[n]=(a-a%nominal[n])/nominal[n];
}
a=a%nominal[n];
n=n+1;
if (n==6)
{break;}
}
cout<<"\n Monet 5zl:"<<ile[0];
cout<<"\n Monet 2zl:"<<ile[1];
cout<<"\n Monet 1zl:"<<ile[2];
cout<<"\n Monet 0,50zl:"<<ile[3];
cout<<"\n Monet 0,20zl:"<<ile[4];
cout<<"\n Monet 0,10zl:"<<ile[5];
cin>>n;
return 0;
}[/code]
Napisałem coś takiego (jest tego trochę więcej bo to projekt na studia)
[code]class Puszka
def initialize(nazwa, cena, ilosc)
@nazwa = nazwa
@cena = cena
@ilosc = ilosc
end
def to_s
“Napoj: #@nazwa, cena: #@cena zl, ilosc: #@ilosc”
end
def wydaj
@ilosc=@ilosc-1
end
def zakup
if ($kasa < @cena) then puts “Za malo pieniedzy”
elsif @ilosc==0 then puts “Brak towaru, przepraszamy”
else self.wydaj
@@reszta=$kasa-@cena
self.obliczreszte
puts “Automat sprzedaje napoj #@nazwa i \nwydaje reszte w wysokosci #@@reszta zl”
end
end
def wplacmonete
n = 0
until n<6
a1=$nominal.at(n)
i=$ile[n]
b=$kasa10
if (b>=a1) then i = ((b-b.divmod(a1))/a1)
end
b=b.divmod(a1)
n=n+1
end
z = 0
until z<6
$stan2[z]=$stan.at(z)+$ile.at(z)
z=z+1
end
end
def obliczreszte
n = 0
until n<6
a1=$nominal.at(n)
i=$ile[n]
b=@@reszta10
if (b>=a1) then i = ((b-b.divmod(a1))/a1)
end
b=b.divmod(a1)
n=n+1
end
z = 0
until z<6
$stan2[z]=$stan.at(z)-$ile.at(z)
z=z+1
end
end
def stanbanku
print "Monet o nominale 5,00zl: "
puts $stan.at(0)
print "Monet o nominale 2,00zl: "
puts $stan.at(1)
print "Monet o nominale 1,00zl: "
puts $stan.at(2)
print "Monet o nominale 0,50zl: "
puts $stan.at(3)
print "Monet o nominale 0,20zl: "
puts $stan.at(4)
print "Monet o nominale 0,10zl: "
puts $stan.at(5)
end
def stanbanku2
print "Monet o nominale 5,00zl: "
puts $stan2.at(0)
print "Monet o nominale 2,00zl: "
puts $stan2.at(1)
print "Monet o nominale 1,00zl: "
puts $stan2.at(2)
print "Monet o nominale 0,50zl: "
puts $stan2.at(3)
print "Monet o nominale 0,20zl: "
puts $stan2.at(4)
print "Monet o nominale 0,10zl: "
puts $stan2.at(5)
end
end
cola =Puszka.new(“Coca-cola”, 2.20, 30)
sprite =Puszka.new(“Sprite”, 1.80, 25)
fanta =Puszka.new(“Fanta”, 2.00, 35)
test =Puszka.new(“Test”, 0, 0)
$stan=Array.new
$stan=[50,50,50,50,50,50]
$stan2=Array.new
$nominal=Array.new
$nominal=[500,200,100,50,20,10]
$ile=Array.new
puts “Trwa sprawdzanie automatu, prosze czekac…”
puts “W automacie znajduja sie obecnie takie napoje:”
print "1. "
puts cola.to_s
print "2. "
puts sprite.to_s
print "3. "
puts fanta.to_s
puts “W automacie znajduja sie obecnie takie monety:”
test.stanbanku
puts “Sprawdzanie zostalo zakonczone sukcesem”
puts
puts
puts
puts “Witamy! Prosze wrzucic monete”
$kasa=gets.to_f
puts “Automat przyjal #$kasa zl”
test.wplacmonete
puts “Prosze wybrac jeden z napojow podanych \nponizej i nacisnac odpowiedni numer na klawiaturze,\nw przypadku rezygnacji z zakupow prosze nacisnac 0”
print "1. "
puts cola.to_s
print "2. "
puts sprite.to_s
print "3. "
puts fanta.to_s
@wybor=gets.to_i
if @wybor==1 then cola.zakup
elsif @wybor==2 then sprite.zakup
elsif @wybor==3 then fanta.zakup
elsif @wybor==0 then puts “Zegnamy”
else puts “Przykro nam, nie mamy takiego napoju”
end
puts “W automacie pozostaly nastepujace napoje:”
print "1. "
puts cola.to_s
print "2. "
puts sprite.to_s
print "3. "
puts fanta.to_s
puts “W automacie znajduja sie obecnie takie monety:”
test.stanbanku2
puts “Nacisnij dowolny klawisz aby kontynuowac…”
@thaend=gets.to_i[/code]
chodzi mi głównie o metody wplacmonete i obliczreszte
Moglby mi ktos udzielic jakis wskazowek jak to wyprostowac by dzialalo wedle oczekiwan?
Metoda wplacmonete ma dodac wrzucona monete do tablicy stan2 i glownie o to mi chodzi, jak juz bede wiedzial jak sobie z tym poradzic to dalej bedzie juz z gorki 
Może lepiej pozostać przy wersji C++? A jeśli już musi być w Rubim, to radzę lepiej opanować jego