Created
October 28, 2011 21:34
-
-
Save philosodad/1323626 to your computer and use it in GitHub Desktop.
7 languages Ruby Day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Hello, world." | |
("hello, Ruby".scan /\w+/).index "Ruby" | |
10.times{puts "my name"} | |
(1..10).each{|k| puts "This is sentence number #{k}"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
I like the use of the regex in the "find the index of Ruby" problem.
It's a skill I lack but as @ Mark Smith said "Make it your 8th language".
Even with all the crap regex cops its still better than the usual horrific-menagerie of manual splits, sub-strings, trims, etc, you see in the average non-regex-proficient coders string manipulation (for example: mine!)
Mine too :-P
I really need to hook into regex. I just find it so opaque!
…On Sat, Oct 29, 2011 at 11:43 AM, jdoig < ***@***.***>wrote:
I like the use of the regex in the "find the index of Ruby" problem.
It's a skill I lack but as @mark Smith said "Make it your 8th language".
Even with all the crap regex cops its still better than the usual
horrific-menagerie of manual splits, sub-strings, trims, etc, you see in the
average non-regex-proficient coders string manipulation (for example mine!)
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1323626
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
I made a couple of mods to this to remove some useless lines and an unnecessary variable assignment.