# Something wrong with case/when syntax

**URL:** <https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018>\
**Category:** Ruby\
**Created:** [June 10, 2008, 4:41am UTC](https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018 "2008-06-10T04:41:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jjc](https://www.ruby-forum.com/u/jjc)\
**Post date:** [June 10, 2008, 4:41am UTC](https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018/1 "2008-06-10T04:41:57Z")

</div>

hi,  
Can You tell me please why this instruction doesn’t want to work  
properly?  
It only repeats the:  
puts “Buy drink: press 1”  
puts “Print money report: press 2”  
puts “Print drink report: press 3”  
puts “Exit: press 0”  
but nothing more

while q!=0  
puts “What to do?”  
puts “Buy drink: press 1”  
puts “Print money report: press 2”  
puts “Print drink report: press 3”  
puts “Exit: press 0”

q = gets  
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
 if drinks[g].czyjest() then
  y=1
      s=0
  while y!=0
            puts "Cash: ",s," What to do: \n\t\t Insert cash: 0

```

\n\t\t Buy drink: 1 \n\t\t Cancel: 2"  
puts "Choose: "  
y = gets  
if y==0 then  
puts "Insert coins. "  
m = gets  
s+=m  
p.add(m)  
end  
if y==1 && drinks[g].givechange(s)\<0 then puts “No money, insert  
coins”  
else  
puts "change: "  
p.monety(drinks,s,g)  
end  
if y==2 then break end  
end

```
  else puts "No such drink."
  end

```

when 2:  
p.write()  
break;  
when 3:  
puts "You bought: "  
for i in 0…2  
drinks[i].namea()  
puts ": ", drinks[i].bought()  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![jjc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jjc](https://www.ruby-forum.com/u/jjc)\
**Post date:** [June 10, 2008, 4:43am UTC](https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018/2 "2008-06-10T04:43:16Z")

</div>

before the while instructions those are set:  
q=1  
m=1  
s=0

---

<div class="post-metadata">

**Author:** ![jjc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jjc](https://www.ruby-forum.com/u/jjc)\
**Post date:** [June 10, 2008, 7:21am UTC](https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018/3 "2008-06-10T07:21:33Z")

</div>

Jed K. wrote:

> while q!=0  
> case q
> 
> when 1:

Kernel#gets returns a string. You are comparing the resulting value to  
integers. Try using gets.strip.to\_i instead.

-Justin

---

<div class="post-metadata">

**Author:** ![jjc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jjc](https://www.ruby-forum.com/u/jjc)\
**Post date:** [June 10, 2008, 11:55am UTC](https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018/4 "2008-06-10T11:55:43Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Tue, 10 Jun 2008, Jed K. wrote:
> 
> > hi,  
> > Can You tell me please why this instruction doesn’t want to work  
> > properly?
> 
> q is a string, and you’re comparing it with integers (0, 1, etc.).
> 
> David

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

---

<div class="post-metadata">

**Author:** ![jjc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jjc](https://www.ruby-forum.com/u/jjc)\
**Post date:** [June 10, 2008, 7:32am UTC](https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018/5 "2008-06-10T07:32:08Z")

</div>

Hi –

On Tue, 10 Jun 2008, Jed K. wrote:

> hi,  
> Can You tell me please why this instruction doesn’t want to work  
> properly?

q is a string, and you’re comparing it with integers (0, 1, etc.).

David

---

<div class="post-metadata">

**Author:** ![jjc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jjc](https://www.ruby-forum.com/u/jjc)\
**Post date:** [June 10, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018/6 "2008-06-10T16:57:34Z")

</div>

2008/6/10 Jed K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> $price=cen
> $ammount=ile
> $name=nn
> $bought=0
> end
> 
> ```

This gives me the creeps: you are modifying global variables like  
instance variables. That’s definitively a don’t do.

Regards

robert

---

<div class="post-metadata">

**Author:** ![jjc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jjc](https://www.ruby-forum.com/u/jjc)\
**Post date:** [June 10, 2008, 3:39pm UTC](https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018/7 "2008-06-10T15:39:53Z")

</div>

Jed K. wrote:

> BTW can somebody check for other errors?

Homework? 😉

I spotted these errors: chash, Iput, Drinku, boughts.

---

<div class="post-metadata">

**Author:** ![jjc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@jjc](https://www.ruby-forum.com/u/jjc)\
**Post date:** [June 10, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/something-wrong-with-case-when-syntax/140018/8 "2008-06-10T18:44:29Z")

</div>

Robert K. wrote:

> 2008/6/10 Jed K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > ```
> > $price=cen
> > $ammount=ile
> > $name=nn
> > $bought=0
> > end
> > 
> > ```
> 
> This gives me the creeps: you are modifying global variables like  
> instance variables. That’s definitively a don’t do.
> 
> Regards
> 
> robert

thx, corrected
