Advertisement
obernardovieira

Finding a Nanny (day 1) [SLiSW]

Jul 20th, 2015
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.38 KB | None | 0 0
  1. =begin
  2. Hello everyone, finally my summer vacations started, so, it's time for a new challenge. This time I will read a book called "Seven Languages in Seven Weeks". Ruby is the first language, and, this is some exercises from day 1. Everyday has exercises, so, I will try to share the most I can. I hope everyone like. Have a nice day ;)
  3. =end
  4.  
  5. # Exercise (1)
  6. puts 'Ola mundo!'
  7.  
  8.  
  9. # Exercise (2)
  10. strToSearch = 'Ola, Ruby'
  11. strToSearch.index('Ruby')
  12.  
  13.  
  14. # Exercise (3)
  15. i = 0
  16. while(i < 10)
  17.     puts 'O meu nome'
  18.     i = i + 1
  19. end
  20.  
  21.  
  22. # Exercise (4)
  23. i = 1
  24. while(i <= 10)
  25.     puts "Este é o número #{i}"
  26.     i = i + 1
  27. end
  28.  
  29.  
  30. # Exercise (5)
  31. #------myruby.rb------
  32.  
  33. #!/usr/bin/env ruby
  34. puts 'Ola mundo'
  35.  
  36. #---------------------
  37.  
  38. =begin
  39. then just "ruby myruby.rb" in command line
  40. or (if using linux)
  41. (a) just be sure your file executable by running "chmod +x myruby.rb"
  42. (b) and run "./myruby"
  43. Thanks to http://stackoverflow.com/questions/8721369/how-to-execute-a-ruby-script-in-terminal
  44. =end
  45.  
  46.  
  47.  
  48. # BONUS Exercise
  49.  
  50. #------guessnumber.rb------
  51. #!/usr/bin/env ruby
  52.  
  53. randNumber = rand(10)
  54. found = 0
  55. puts 'Which number between 0 and 10 the program chosen?'
  56. begin
  57.     input = gets
  58.     number = input.chomp.to_i
  59.     if number == randNumber then
  60.         puts 'Congrats! You found the number!'
  61.         found = 1
  62.     else
  63.         puts 'Wrong answer, try again.'
  64.     end
  65. end until found == 1
  66. #---------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement