Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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.
- # Some Arrays
- []
- [5]
- ['Hello', 'Goodbye']
- flavor = 'vanilla' # This is not an array, of course...
- [18.9, flavor, [true, false]] # ... but this is.
- # 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:
- names = ['Ada', 'Belle', 'Chris']
- puts names
- puts names[0]
- puts names[1]
- puts names[2]
- 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'.
- # '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:
- languages = ['English', 'German', 'Ruby']
- languages.each do |lang|
- puts 'I love ' + lang + '!'
- puts 'Don\'t you?'
- end
- puts 'And let\'s hear it for C++!'
- puts '...'
- # 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'.
- # 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!
- 3.times do
- puts 'Hip-Hip-Hooray!'
- end
- # Other Array Methods - almost as many as string methods
- # to_s and join - join works much like to_s does, except that it adds a string in between the array's objects.
- foods = ['artichoke', 'brioche', 'caramel']
- puts foods
- puts
- puts foods.to_s
- puts
- puts foods.join(', ')
- puts
- puts foods.join(' :) ') + ' 8)'
- 200.times do
- puts []
- end
- # 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.
- # 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:
- favorites = []
- favorites.push 'raindrops on roses'
- favorites.push 'whiskey on kittens'
- puts favorites[0]
- puts favorites.last
- puts favorites.length
- puts favorites.pop
- puts favorites
- puts favorites.length
- # 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!
- names = []
- x = 'temporary'
- puts 'Please enter any amount of words, and I will alphebetize them for you! :)'
- while x != ''
- x = gets.chomp
- if x != ''
- names.push x
- else
- x = ''
- end
- end
- puts 'Let\'s sort these.'
- sorted = []
- names.each do |n|
- if n >= 'A'
- sorted.push n
- end
- end
- puts sorted
- # 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!
- # WOULD NEED TO SORT ALL MYSELF INSTEAD OF USING THE METHOD. HELP? NECESSARY??
- # 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.
- toc = ['Table of Contents', 'Chapter 1', 'Getting Started',
- 'page 1', 'Chapter 2', 'Numbers', 'page 9',
- 'Chapter 3', 'Letter', 'page 13']
- line_width = 30
- puts( toc[0].center(line_width*3))
- puts
- puts( toc[1].ljust(line_width) +
- toc[2].ljust(line_width) + toc[3].ljust(line_width))
- puts( toc[4].ljust(line_width) +
- toc[5].ljust(line_width) + toc[6].ljust(line_width))
- puts( toc[7].ljust(line_width) +
- toc[8].ljust(line_width) + toc[9].ljust(line_width))
Add Comment
Please, Sign In to add comment