Learning Ruby - 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Learning Ruby - 2

Description:

Ruby arrays shrink and grow dynamically - no more annoying array ... my_a.flatten. my_a.flatten! my_a.each { |element| puts element if element.length 4 } ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 9
Provided by: glasnost
Category:
Tags: flatten | learning | ruby

less

Transcript and Presenter's Notes

Title: Learning Ruby - 2


1
Learning Ruby - 2
  • Ruby Arrays and Lists

2
Ruby Arrays are Cool!
  • We've already seen a Ruby array song_lines
  • Ruby arrays shrink and grow dynamically - no more
    annoying array index errors.
  • Ruby array elements can contain any other object
    (think about the implications of this!)
  • Ruby arrays can be set with a list of items

3
Playing with Ruby Arrays
  • my_a 'one', 'two', 'three'
  • puts my_a1
  • other_a "four", "five", "six"
  • puts other_a0
  • my_a3 other_a
  • puts my_a32
  • other_a3 "seven"
  • my_a

4
Fun with Indices
  • puts other_a-1
  • another_a other_a1..2
  • another_a
  • another_a.values_at(0, 2)
  • my_a5 this is the end
  • my_a.values_at(1, 3, 5).sort

5
Ruby Array Methods
  • puts another_a.length
  • puts other_a.length
  • puts my_a.length
  • Array.new
  • Array.new(100)
  • Array.new(10, 'x')
  • other_a.sort
  • other_a.delete("five")
  • other_a
  • my_a

6
Working with "each" Element
  • my_a.each element puts element if
    element.length gt 4
  • my_a.each element puts element if
    element.length gt 3
  • my_a.each element puts element if
    element.length gt 2
  • my_a.flatten
  • my_a.flatten!
  • my_a.each element puts element if
    element.length gt 4

7
Ruby Array Stacks
  • last my_a.pop
  • first my_a.shift
  • my_a
  • my_a.unshift( last )
  • my_a.push( first )
  • my_a

8
More ... Ruby So Far
  • Ruby arrays are cool!
  • The array methods are cool!
  • The array iterators are cool!
  • Wave "bye bye" to implementing scary linked
    lists!!!!
  • OK, all together now Ruby arrays are cool!
Write a Comment
User Comments (0)
About PowerShow.com