TKVR

Chris Pine, Learn to Program Chapter 5 - Methods

Feb 16th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.52 KB | None | 0 0
  1. # 5 + 5 is really just a shortcut way of writing 5.+ 5
  2. puts 'hello '. + 'world'
  3. puts (10.* 9).+ 9
  4.  
  5. # if I say  puts 'to be or not to be', what I am really saying is  self.puts 'to be or not to be'. So what is self? It's a special variable which points to whatever object you are in. (ex. "Die!" -> implicit noun is whoever you are saying it to. My computer tells me when don't need the 'self'
  6.  
  7. # iCantBelieveIMadeAVariableNameThisLongJustToPointToA3 = 3
  8. # puts iCantBelieveIMadeAVariableNameThisLongJustToPointToA3
  9. # self.puts iCantBelieveIMadeAVariableNameThisLongJustToPointToA3
  10.  
  11. # fancy string methods (reference method documents for ruby, just like a dictionary is for words)
  12. # reverse
  13. var1 = 'stop'
  14. var2 = 'stressed'
  15. var3 = 'Can you pronounce this sentence backwards?'
  16.  
  17. puts var1.reverse
  18. puts var2.reverse
  19. puts var3.reverse
  20. puts var1
  21. puts var2
  22. puts var3
  23.  
  24. #length
  25. puts 'What is your full name?'
  26. name = gets.chomp
  27. puts 'Did you know there are ' + name.length.to_s + ' characters in your name, ' + name + '?'
  28.  
  29. # write a program which asks for your first, middle, and last names individually, and then adds those lengths together
  30. puts 'What is your first name?'
  31. firstName = gets.chomp
  32. puts 'What is your middle name?'
  33. middleName = gets.chomp
  34. puts 'What is your last name?'
  35. lastName = gets.chomp
  36. fullName = firstName.length.to_i + middleName.length.to_i + lastName.length.to_i
  37. puts 'Did you know there are ' + fullName.to_s + ' characters in your name?'
  38.  
  39. # change the case (upper/lowercase) of a string
  40. letters = 'aAbBcCdDeE'
  41. puts letters.upcase
  42. puts letters.downcase
  43. puts letters.swapcase
  44. puts letters.capitalize
  45. puts ' a'.capitalize # only capitalizes the first character, not the first letter
  46. puts letters
  47.  
  48. #visual formatting
  49. # .center the lines of a poem
  50. lineWidth = 50
  51. puts('Old Mother Hubbard'.center(lineWidth))
  52. puts('Sat in her cupboard'.center(lineWidth))
  53. puts('Eating her curs an whey,'.center(lineWidth))
  54. puts('When along came a spider'.center(lineWidth))
  55. puts('Which sat down beside her'.center(lineWidth))
  56. puts('And scared her poor shoe dog away.'.center(lineWidth))
  57.  
  58. # left justify and right justify -> pad on either side
  59. # center, ljust, & rjust
  60. lineWidth = 40
  61. str = '--> text <--'
  62. puts str.ljust lineWidth
  63. puts str.center lineWidth
  64. puts str.rjust lineWidth
  65. puts str.rjust (lineWidth/2)
  66.  
  67. # Write an Angry Boss program. It should rudely ask what you want. Whatever you answer, the Angry Boss should yell it back to you, and then fire you. For example, if you type in [I want a raise.],
  68. # it should yell back "WHADDAYA MEAN "I WANT A RAISE."?!?  YOU'RE FIRED!!"
  69. puts 'Uh oh! The boss is angry today.'
  70. puts 'Tread carefully. . .'
  71. puts 'Let\'s see if you can land a raise.'
  72. puts 'Ask by typing: I want a raise.'
  73. puts 'Angry Boss says: "What do you want!?"'
  74. raise = gets.chomp
  75. puts 'WHADDYA MEAN "I WANT ' + raise.upcase + '" ?!? YOU\'RE FIRED!"'
  76.  
  77. # Write a program which will display a Table of Contents so that it looks like this:
  78. #                 Table of Contents                
  79. #                                                
  80. # Chapter 1:  Numbers                        page 1
  81. # Chapter 2:  Letters                       page 72
  82. # Chapter 3:  Variables                    page 118
  83. page_width = 60
  84. puts ('Table of Contents'.center(page_width))
  85. puts ''
  86. puts ('Chapter 1: Getting Started'.ljust(page_width/2) + 'page 1'.rjust(page_width/2))
  87. puts ('Chapter 2: Numbers'.ljust(page_width/2) + 'page 9'.rjust(page_width/2))
  88. puts ('Chapter 3: Letters'.ljust(page_width/2) + 'page 13'.rjust(page_width/2))
Add Comment
Please, Sign In to add comment