Hourglass Contracts & Milestone Invoicing

Hourglass is a web-based invoicing application that makes sure you get paid, on time and every time.

Collect deposits, deliver milestone invoices and start every project with one of our contract templates

5 Things Highly-Paid Rails Consultants Do Differently

With this valuable course, you'll learn the secrets of how to:

  • Regain valuable hours back by hacking your workflow
  • Build a one-man consultancy and work wherever you want
  • Attract high-paying clients you'll love working with, every time
  • Build passive income by productizing what you're already selling!
P.S.: We'll send you actionable advice. No junk. Unsubscribe anytime.

Free Guide!

Five Things Highly-Paid Freelancers Do Differently

Using Ruby: How To Add Elements To An Array

In Ruby, there's almost always more than one way to do something.

Interacting with arrays is no different. For those of you who are new to programming, the Ruby Docs define an array as "...ordered, integer-indexed collections of any object."

You could have an array or integers, strings, or a combination of both. Let's take a look at a sample array:

cheeses = Array.new
=> []

Jeez, that's one boring array. Let's spice things up and start adding to it.

cheeses << "gouda"
=> ["gouda"]
cheeses << "cheddar"
=> ["gouda", "cheddar"]

That looks better, now our array cheeses isn't quite so empty. Did you notice that << symbol? That's a synonym for the push method, which tacks something on to the end of an array. We would get the same result if we used the push method and passed in a string as an argument.

cheeses
=> ["gouda", "cheddar"]
# let's use push instead of <<
cheeses.push("mascarpone")
=> ["gouda", "cheddar", "mascarpone"]

Pretty awesome! Our cheeses array is starting to look good (and delicious).

Adding Elements to an Array in Specific Spots

Remember, arrays are an ordered list. Meaning the first spot in our array (which is actually spot 0. In most programming languages, ordered indexes start at 0. Which means that the second element in the array is actually in the 1 slot) is currently "gouda".

If we want to "bump" all of our cheeses ahead one spot and replace the [0] slot with a different cheese, we can use the unshift method.

cheeses
=> ["gouda", "cheddar", "mascarpone"]
# let's put mozzarella in the first spot of our array with the unshift method
cheeses.unshift("mozzarella")
=> ["mozzarella", "gouda", "cheddar", "mascarpone"]

Awesome! Mozzarella is like the king of the cheeses array now.

Now let's pretend that we're add something else to array but we want it in the second (or the [1]) spot.

# we need to add gruyere into the second spot
cheeses[1] = "gruyere"
=> "gruyere"
cheeses
=> ["mozzarella", "gruyere", "cheddar", "mascarpone"]

See how we specified [1] when we were altering our cheeses array? We could have also easily put "gruyere" into the third spot with [2] or the first spot with [0]. But what happened to "gouda"? It looks like we replaced it with "gruyere" when we specified the index.

If we're trying to insert an object into an array (without replacing a different object) we need to use the insert method.

# let's add gouda into the third spot
cheeses.insert(2, "gouda")
=> ["mozzarella", "gruyere", "gouda", "cheddar", "mascarpone"] 

Much better. Great! Now we know how to add elements to the end of an array, the beginning of an array, and even a specific spot in an array. Fire up irb and give it a whirl.

By the way, did you catch our last lesson on how to sort hashes in Ruby? If not, you should check it out!

5 Things Highly-Paid Rails Consultants Do Differently

With this valuable course, you'll learn the secrets of how to:

  • Regain valuable hours back by hacking your workflow
  • Build a one-man consultancy and work wherever you want
  • Attract high-paying clients you'll love working with, every time
  • Build passive income by productizing what you're already selling!
P.S.: We'll send you actionable advice. No junk. Unsubscribe anytime.

Free Guide!

Five Things Highly-Paid Freelancers Do Differently