7stud2
(7stud --)
1
Currently doing exercise 35 on Learn Ruby the hard way.
http://ruby.learncodethehardway.org/book/ex35.html
Heres the piece of code that I'm playing with:
def gold_room()
puts "This room is full of gold. How much do you take?"
prompt; next_move = gets.chomp
if next_move.include? "0" or next_move.include? "1"
how_much = next_move.to_i()
else
dead("Man, learn to type a number.")
end
Currently if the user inputs "0 Gold" or "1 Gold" it would convert that
string to an integer.
But if the user inputs "2 Gold" or "3 Gold" etc. it would return dead.
How do I make it so that it is not limited to numbers 0 and 1?
Thanks!
···
--
Posted via http://www.ruby-forum.com/.
Robert_K1
(Robert K.)
2
if /^(\d+)\s+(\S.*)$/ =~ next_move
how_much = Integer($1)
where = $2
...
else
dead "Wrong input"
end
Kind regards
robert
···
On Sun, Feb 23, 2014 at 12:46 PM, Michael Sorm <lists@ruby-forum.com> wrote:
Currently doing exercise 35 on Learn Ruby the hard way.
Learn Ruby the Hard Way
Heres the piece of code that I'm playing with:
def gold_room()
puts "This room is full of gold. How much do you take?"
prompt; next_move = gets.chomp
if next_move.include? "0" or next_move.include? "1"
how_much = next_move.to_i()
else
dead("Man, learn to type a number.")
end
Currently if the user inputs "0 Gold" or "1 Gold" it would convert that
string to an integer.
But if the user inputs "2 Gold" or "3 Gold" etc. it would return dead.
How do I make it so that it is not limited to numbers 0 and 1?
--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/