How do I change change string to integer?

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!

On Sun, Feb 23, 2014 at 12:46 PM, Michael S. [email protected]
wrote:

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?

if /^(\d+)\s+(\S.*)$/ =~ next_move
how_much = Integer($1)
where = $2

else
dead “Wrong input”
end

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs