Forum: Ruby Programme.I can't get it

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
end
def setAddress(adr)
@address  =  adr
end
def getName()
return  @name
end
def getAddrs()
return  @address
end
end

puts "Enter name of persons"
personel = Array.new
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 = Person.new
person.setName(name)
personel.push(person)
iftype
elsif (select == 2)
  for  i  in  0..personel.length()-1
puts "Person number" + i.to_s + " " + personel[i].getName()
end
elsif (select ==3)
puts "Drink number"
puts "\n" + "Menu"
firstHash = Hash.new()
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 = Hash.new()
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"

end
end

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'
end
end

versus

class Foo
  def hi
    puts 'hi'
  end
end

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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.