TKVR

Chris Pine, Learn to Program Chapter 7 - Arrays

Feb 16th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.63 KB | None | 0 0
  1. # Array = list in your computer. Every slot in the list acts like a variable: you can see what object a particular slot points to, and you can make it point to a different object.
  2.  
  3. # Some Arrays
  4. []
  5. [5]
  6. ['Hello', 'Goodbye']
  7.  
  8. flavor = 'vanilla' #  This is not an array, of course...
  9. [18.9, flavor, [true, false]] # ... but this is.
  10.  
  11. # To help us find a particular object in an array, each slot is given an index number. The first slot in the array is slot zero. Here's how we would reference the objects in an array:
  12. names = ['Ada', 'Belle', 'Chris']
  13.  
  14. puts names
  15. puts names[0]
  16. puts names[1]
  17. puts names[2]
  18. puts names[3] # This is out of range. Asked a question and the answer is nothing- not an error. nil means nothing. 'nil' in Ruby is  a special object which basiclly means 'not any other object'.
  19.  
  20. # 'each' Method - allows us to do something (whatever we want) to each object the array points to. So, if we want to say something nice about each language in the array below, we'd do this:
  21. languages = ['English', 'German', 'Ruby']
  22.  
  23. languages.each do |lang|
  24.   puts 'I love ' + lang + '!'
  25.   puts 'Don\'t you?'
  26. end
  27.  
  28. puts 'And let\'s hear it for C++!'
  29. puts '...'
  30. # So- for 'each' object in 'languages', point the variable 'lang' to the object and then 'do' everything I tell you to, until you come to the 'end'.
  31.  
  32. # while, end, do, if, else are not methods. However, 'each' is an array method. Methods like each which "act like" loops are often called iterators. One thing to notice about iterators is that they are always followed by do...end.  while and if never had a do near them; we only use do with iterators. Here's another cute little iterator, but it's not an array method... it's an integer method!
  33. 3.times do
  34.   puts 'Hip-Hip-Hooray!'
  35. end
  36.  
  37. # Other Array Methods - almost as many as string methods
  38. # to_s and join - join works much like to_s does, except that it adds a string in between the array's objects.
  39. foods = ['artichoke', 'brioche', 'caramel']
  40.  
  41. puts foods
  42. puts
  43. puts foods.to_s
  44. puts
  45. puts foods.join(', ')
  46. puts
  47. puts foods.join('  :)  ') + '  8)'
  48.  
  49. 200.times do
  50.   puts []
  51. end
  52. # puts treats arrays differently from other objects: it just calls puts on each of the objects in the array. That's why putsing an empty array 200 times doesn't do anything; the array doesn't point to anything, so there's nothing to puts.
  53.  
  54. # push, pop, & last -  The methods push and pop are sort of opposites, like + and - are.  push adds an object to the end of your array, and pop removes the last object from the array (and tell you what it was).  last is similar to pop in that it tells you what's at the end of the array, except that it leaves the array alone. Again, push and pop actually change the array:
  55. favorites = []
  56. favorites.push 'raindrops on roses'
  57. favorites.push 'whiskey on kittens'
  58.  
  59. puts favorites[0]
  60. puts favorites.last
  61. puts favorites.length
  62.  
  63. puts favorites.pop
  64. puts favorites
  65. puts favorites.length
  66.  
  67. # Let's write a program which asks us to type in as many words as we want (one word per line, continuing until we just press Enter on an empty line), and which then repeats the words back to us in alphabetical order. Hint: There's a lovely array method which will give you a sorted version of an array:  sort. Use it!
  68. names = []
  69. x = 'temporary'
  70.  
  71. puts 'Please enter any amount of words, and I will alphebetize them for you! :)'
  72.  
  73. while x != ''
  74.   x = gets.chomp
  75.   if x != ''
  76.     names.push x
  77.   else
  78.     x = ''
  79.   end
  80. end
  81.  
  82. puts 'Let\'s sort these.'
  83.  
  84. sorted = []
  85.  
  86. names.each do |n|
  87.   if n >= 'A'
  88.     sorted.push n
  89.   end
  90. end
  91.  
  92. puts sorted
  93.  
  94. # Try writing the above program without using the sort method. A large part of programming is solving problems, so get all the practice you can!
  95.  
  96. # WOULD NEED TO SORT ALL MYSELF INSTEAD OF USING THE METHOD. HELP? NECESSARY??
  97.  
  98. # Rewrite your Table of Contents program (from the chapter on methods). Start the program with an array holding all of the information for your Table of Contents (chapter names, page numbers, etc.). Then print out the information from the array in a beautifully formatted Table of Contents.
  99. toc = ['Table of Contents', 'Chapter 1', 'Getting Started',
  100.         'page 1', 'Chapter 2', 'Numbers', 'page 9',
  101.         'Chapter 3', 'Letter', 'page 13']
  102.  
  103.         line_width = 30
  104.  
  105.         puts(              toc[0].center(line_width*3))
  106.         puts
  107.         puts(              toc[1].ljust(line_width) +
  108. toc[2].ljust(line_width) + toc[3].ljust(line_width))
  109.         puts(              toc[4].ljust(line_width) +
  110. toc[5].ljust(line_width) + toc[6].ljust(line_width))
  111.         puts(              toc[7].ljust(line_width) +
  112. toc[8].ljust(line_width) + toc[9].ljust(line_width))
Add Comment
Please, Sign In to add comment