Skip to content

Instantly share code, notes, and snippets.

@mikedao
Last active June 8, 2016 14:11
Show Gist options
  • Save mikedao/40094a86e5d5ef9317683b4c2a0860bb to your computer and use it in GitHub Desktop.
Save mikedao/40094a86e5d5ef9317683b4c2a0860bb to your computer and use it in GitHub Desktop.

Time to practice. I'm going to give you two arrays.

  chocolate = ["Ritual",
                "Chuao",
                "Chocolove",
                "Scharffen Berger"]
  peanut_butter = ["Peter Pan",
                    "Skippy",
                    "Justin's",
                    "Smucker's",
                    "Crazy Richard's"]

I want you to zip them and then print out to the screen,

  "You got your Ritual in my Peter Pan!"
  "You got your Peter Pan in my Ritual!"
  "You got your Chuao in my Skippy!"

Now let's practice again with some real world data. This is something that you'll often get. Someone writes some pretty poor software, and you get two associated arrays, but you need to actually put it together.

People are the worst.

We have two arrays from a form.

people = ["Hannah",
          "Penelope",
          "Rabastan",
          "Neville",
          "Tonks",
          "Seamus"]

houses = ["Hufflepuff",
          "Ravenclaw",
          "Slytherin",
          "Gryffindor",
          "Hufflepuff",
          "Gryffindor"]

Some jerk left us with two arrays. Take the next few minutes to put them together and then I would like you to print them out to the screen in this kind of format:

  "Hannah is in Hufflepuff."
  "Penelope is in Ravenclaw."

  people.zip(houses).each do |name, house|
    puts "#{name} is in #{house}
  end

And so forth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment