Forum: Ruby Programme.I can't get it

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
79ed8517fe482afed2757487523c10d1?d=identicon&s=25 Mandon Sergiu (daniel_d43)
on 2016-11-10 20:49
I need a little help in creating a programme that is able  keep track of
how much an individual consumes at the bar. It shall separate between
soft drinks, bear and drinks.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-11-11 08:01
So, ask specific questions. If you have several question, it is better
to create separate postings for each posting.

From the wording of your question, I am unsure how much experience you
have in programming, so it would help if you also tell a bit of how deep
your knowledge is in programming in general, and what programming
languages (i.e. not Ruby) you are comfortable with.
79ed8517fe482afed2757487523c10d1?d=identicon&s=25 Mandon Sergiu (daniel_d43)
on 2016-11-12 10:44
class  Person
def setName(name)
@name  =  name
def setAddress(adr)
@address  =  adr
def getName()
return  @name
def getAddrs()
return  @address

puts "Enter name of persons"
personel =
select = 0
while (select !=4)
puts "Press 1 to enter drink name"
puts "Press 2 to see the drinks tracked"
puts "Press 3 to show the menu of driks available"
puts "Press 4 to exit the programme"
select = gets.chomp.to_i
if (select==1)
puts "Type what drink do you prefer"
name = gets.chomp
person =
elsif (select == 2)
  for  i  in  0..personel.length()-1
puts "Person number" + i.to_s + " " + personel[i].getName()
elsif (select ==3)
puts "Drink number"
puts "\n" + "Menu"
firstHash =
puts "\n" + firstHash.inspect + "- Drinks"
firstHash["1"] = "Beer"
firstHash["2"] = "Whisky"
firstHash["3"] = "Vodka"
#puts firstHash.values
#puts firstHash.keys
firstHash.each{|key, value| puts "#{key} \t is \t #{value}"}

firstHash =
puts "\n" + firstHash.inspect + "- SoftDrinks"
firstHash["4"] = "Tea"
firstHash["5"] = "Coke"
firstHash["6"] = "Pepsi"

#puts firstHash.values
#puts firstHash.keys
firstHash.each{|key, value| puts "#{key} \t is \t #{value}"}

elsif (select>4)
puts "Not a correct selection"


This is my programme. When I press 1 for typing new drinks i want to put
a condition to be able to type just my drinks available in menu... if
not to put a message with a response like: a don't have this drink
79ed8517fe482afed2757487523c10d1?d=identicon&s=25 Mandon Sergiu (daniel_d43)
on 2016-11-12 10:47
I am a beginner on Ruby ... so I'd be thankfull for a liittle help
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-11-14 12:29
The program is too hard to read without proper indentation. Please edit
your posting accordingly.

Also, I don't quite understand your question. Maybe you could also
provide an example dialogue, which you would like to achieve. This would
maybe clarify your problem a bit.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2016-11-15 00:25
Hello Mandon Sergiu,

I would put stuff such as:

    puts "Enter name of persons"

into your specific class. Give it
some logical name such as "def user_input"
or something like that. You can put this
method then inside of "def initialize",
or you can also call a specialized method
in your own. I usually call a method
called "run" from within "def initialize"
methods for my larger classes. It helps
my brain remember - .run on an object
and it will run the main-action. (The
rest is mostly for initializing the
class and sanitizing it if necessary)

Also try to indent the code - being a beginner
is no problem but being unable to indent code
is a big problem. :D

class Foo
def hi
puts 'hi'


class Foo
  def hi
    puts 'hi'

Do you see the difference?

It will help other people help you more easily
by the way because that way, they can scan and
spot/identify problems with code much more
rapidly so.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2016-11-15 07:32
@Robert Heiler: While I agree, that in a good OO design, the whole
user-interaction should go in its own class (say: Interactor), maybe
this concept is a bit too difficult for a complete beginner in
programming. I have the impression that the OP is still struggling with
putting together basic statements (scripting), so maybe it would be
easier for him to first put the whole dialgoue into the "main program"
(as he already planned) and, if this works, refactor it later by
designing a suitable Interactor class.
This topic is locked and can not be replied to.