Skip to content

Instantly share code, notes, and snippets.

@yangsu
Created January 28, 2013 03:41
Show Gist options
  • Select an option

  • Save yangsu/4652835 to your computer and use it in GitHub Desktop.

Select an option

Save yangsu/4652835 to your computer and use it in GitHub Desktop.
Ruby Arrays
[1, "two", 3] # is perfectly valid
Array.new # remember, these are objects, and objects can be instantiated
# with .new()
cars = ['ford', 'toyota', 'subaru']
cars[0] # => "ford"
cars[2] # => "subaru"
cars[-1] # => "subaru"
cars[-4] # => nil
cars[9001] # => nil
cars[1..2] # => ["toyota", "subaru"]
cars[1..2] # => ["toyota", "subaru"]
cars[0...2] # => ["ford", "toyota"]
cars.[](0...2)# => ["ford", "toyota"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment