Created
September 29, 2011 17:13
-
-
Save japaz/1251304 to your computer and use it in GitHub Desktop.
7L7W 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
#!/usr/bin/env ruby | |
# Print the string “Hello, world.” | |
puts 'Hello, world' | |
# For the string “Hello, Ruby,” find the index of the word “Ruby.” | |
'Hello, Ruby.'.index('Ruby') | |
# Print your name ten times | |
10.times do | |
puts 'Alberto' | |
end | |
#Print the string “This is sentence number 1,” where the number 1 changes from 1 to 10 | |
for i in 1..10 | |
puts "This is sentence number #{i}" | |
end | |
#Run a Ruby program from a file. | |
#Including the #!/usr/bin/env ruby | |
=begin | |
Bonus problem: If you’re feeling the need for a little more, write | |
a program that picks a random number. Let a player guess the | |
number, telling the player whether the guess is too low or too high. | |
=end |
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
#!/usr/bin/env ruby | |
=begin | |
Bonus problem: If youre feeling the need for a little more, write | |
a program that picks a random number. Let a player guess the | |
number, telling the player whether the guess is too low or too high. | |
(Hint: rand(10) will generate a random number from 0 to 9, and | |
gets will read a string from the keyboard that you can translate to | |
an integer.) | |
=end | |
number_to_guess = rand(10)+1 | |
guessed = 11 | |
puts 'Guess a number between 1 and 10' | |
while guessed != number_to_guess do | |
guessed = gets.to_i | |
puts 'number is greater' if number_to_guess > guessed | |
puts 'number is lower' if number_to_guess < guessed | |
puts 'Congratulatios, you found the number' if number_to_guess == guessed | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment