Stick ’em up! Get what you want out of Arrays.

Let’s talk about how to access things in Arrays.

#[]

This can accept a single integer, which represents the index you wish to hit. Arrays are zero based, meaning that the “first” element is not in the “1” spot, but “0”. If the number is negative, it starts from the end of the array.

invitation = ["dreamer", "wisher", "liar"]
=> ["dreamer", "wisher", "liar"]
invitation[0]
=> "dreamer"
invitation[1]
=> "wisher"
invitation[-1]
=> "liar"
invitation[-2]
=> "wisher"

It can also accept two numbers where the first says where to start and the second argument gives the length to return. Let’s start at the “1” index, return 3 items…

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation[1,3]
=> ["wisher", "liar", "hope-er"]

Start at the end (“-1”), return 3, but there’s nothing after, so…

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation[-1,3]
=> ["magic bean buyer"]

Start at the beginning, give more than there are…

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation[0,7]
=> ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

Finally, we can give two numbers with a twist to get a range of indexes.

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation[1..3]
=> ["wisher", "liar", "hope-er"]

We can still give negative indexes…

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation[1..-2]
=> ["wisher", "liar", "hope-er", "pray-er"]

#slice

A lovely sibling of #[], #slice can do anything that #[] can do.

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.slice(0)
=> "dreamer"

invitation.slice(-2)
=> "pray-er"

invitation.slice(1, 3)
=> ["wisher", "liar", "hope-er"]

invitation.slice(3..7)
=> ["hope-er", "pray-er", "magic bean buyer"]

One thing to note for both #[] and #slice is that while you can give a range outside the array’s size without qualm, you will be spat a nil when you give a single index that, well, doesn’t exist…

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation[7]
=> nil

#slice!

Sure, it may seem like the same method we just went through, just with a little more excitement, but I’m here to confirm it’s more than just pep.

This actually removes what you are, well, slicing out of the array. Whose to say if it’s been changed for the better, but the array has been change for gooooooooood 🎵

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.slice!(0)
=> "dreamer"

invitation
=> ["wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.slice!(0..2)
=> ["wisher", "liar", "hope-er"]

invitation
=> ["pray-er", "magic bean buyer"]

invitation.slice!(0, 2)
=> ["pray-er", "magic bean buyer"]

invitation
=> []

Well, this is going to be a lonely party. Same rules apply regarding nil – you can go out of range when giving a, well, range, but don’t give an index if the limit index does not exist.

tumblr_nhsimxygvn1tk1ch1o1_500

#at

Next up is #at, which isn’t as powerful as #[] or #slice because it can only take one argument. No ranges or finicky orders of “start here, give me this much” here.

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.at(1)
=> "wisher"
invitation.at(-1)
=> "magic bean buyer"

#fetch

If you don’t like getting nil back and you want better communication in your Array relationship, the #fetch is good at expressing its feelings.

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.fetch(5)
=> "magic bean buyer"

invitation.fetch(6)
IndexError: index 6 outside of array bounds: -6...6

No ranges or dual arguments are too overwhelming for emotional #fetch, so stick to just the index here or, be a good human and communicate back what you want in these circumstances.

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.fetch(75, "We're all out of that, can I interest you in something else?")
=> "We're all out of that, can I interest you in something else?"

A double scoop ice cream would suffice, #fetch.

#first and #last

This is Ruby after all, so what would things be without user friendly methods that do exactly what you expect them to do.

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.first
=> "dreamer"

invitation.last
=> "magic bean buyer"

Bonus points for combining Array’s #first (which returns “dreamer”) with String’s #last (which will take “dreamer” from #first and looks to the last ‘index’ there)…

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.first.last
=> "r"

tenor

#take

To grab the first of an Array with something a bit more friendly than #[0, 5], use #take. Note that this is not an index, but the number that you want, which would start at “1”, not “0”. It also simply returns the items without affecting the array itself.

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.take(0)
=> []

invitation.take(5)
=> ["dreamer", "wisher", "liar", "hope-er", "pray-er"]

invitation
=> ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

#drop

As caring as Ruby is, there is no #give to make up for its #take, but you do have the option to drop the first n like they’re hot and return the rest. Again, no permanent altering here, the array is still the same.

invitation = ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

invitation.drop(3)
=> ["hope-er", "pray-er", "magic bean buyer"]

invitation
=> ["dreamer", "wisher", "liar", "hope-er", "pray-er", "magic bean buyer"]

 

And that’s accessing arrays.

The example array is inspired one of Shel Silverstein’s Poems, “Invitation”:

If you are a dreamer, come in,
If you are a dreamer, a wisher, a liar,
A hope-er, a pray-er, a magic bean buyer…
If you’re a pretender, come sit by my fire
For we have some flax-golden tales to spin.
Come in!
Come in!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s