Skip to content

Instantly share code, notes, and snippets.

@philosodad
Created October 28, 2011 21:34
Show Gist options
  • Save philosodad/1323626 to your computer and use it in GitHub Desktop.
Save philosodad/1323626 to your computer and use it in GitHub Desktop.
7 languages Ruby Day 1
puts "Hello, world."
("hello, Ruby".scan /\w+/).index "Ruby"
10.times{puts "my name"}
(1..10).each{|k| puts "This is sentence number #{k}"}
def start_game
puts "Do you want to play?"
x = gets.chomp
if x == 'y'
play_game rand(10)
elsif x == 'n'
exit
end
end
def play_game answer
puts "Please enter your guess"
evaluate_guess gets.chomp, answer
end
def evaluate_guess guess, answer
if guess == 'quit'
exit
elsif guess.to_i < answer
puts "Your guess is too low"
play_game answer
elsif guess.to_i > answer
puts "Your guess is too high"
play_game answer
elsif guess.to_i == answer
puts "You guessed it!"
start_game
else
puts "I didn't expect that."
exit
end
end
start_game
def start_game
print "Do you want to play? (y/n): "
play_game (rand(10) + 1) if gets.chomp =~ /y/
end
def play_game answer
print "Please enter your guess: "
evaluate_guess gets.chomp, answer
end
def evaluate_guess guess, answer
if guess == 'quit'
exit
elsif guess.to_i < answer
puts "Your guess is too low"
play_game answer
elsif guess.to_i > answer
puts "Your guess is too high"
play_game answer
elsif guess.to_i == answer
puts "You guessed it!"
start_game
end
end
start_game
def start_game
puts "Do you want to play?"
play_game (rand(10) + 1) if gets.chomp =~ /^(y|Y)/
end
def play_game answer
print "Please enter your guess: "
evaluate_guess gets.chomp, answer
end
def evaluate_guess guess, answer
if guess == 'quit' then exit end
guess.to_i == answer ? lambda {puts "Your guess is correct"; start_game}.call : next_try(guess.to_i, answer)
end
def next_try guess, answer
guess < answer ? lambda {puts "Your answer is too low!"; play_game answer}.call : lambda {puts "Your answer is too high!"; play_game answer}.call
end
start_game
@philosodad
Copy link
Author

The thing that makes the regex work in the index problem is the 'scan' method, which I found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment