Iterating over a 2-D array in Ruby

I’ve had trouble understanding how nested .each loops work, so I did some investigating and expanded upon (and hopefully clarified) an example from Codeacademy. 

Let’s start with a 2-D array.

s = [[“ham”, “swiss”], [“turkey”, “cheddar”], [“roast beef”, “gruyere”]]

The instructions were to iterate over s in such a way that we don’t print out each element, like ["ham", "swiss"], but each element within each element, so we get a list of all the meats and cheeses within s.

First, let’s do the opposite and print out each element in the 2D array like [“ham”, “swiss”]. To do that, we use only one .each loop. In this case, puts is called only 3 times. We can prove this by marking the puts statement with “>>>”.

s.each do |x|
   puts “>>>”, x
   end

>>>
ham
swiss
>>>
turkey
cheddar
>>>
roast beef
gruyere

Now let’s use nested .each iterators/loops to iterate through a 2D array (to actually go inside each element within each element). With nested .each iterators we are calling puts 6 times. We can show this again by marking the puts statement with “>>>”.

s = [[“ham”, “swiss”], [“turkey”, “cheddar”], [“roast beef”, “gruyere”]]

s.each do |x|
      x.each do |y|
      puts “>>>”, y
      end
end

>>>
ham
>>>
swiss
>>>
turkey
>>>
cheddar
>>>
roast beef
>>>
gruyere

To summarize, the difference between one .each loop and nested .each loops is the latter permeates the dimensions of the array; it prints the elements within each element separately, calling puts twice the number of times.

Git K-holes and Ruby flashcards

This is gonna be a short post because I have to watch The Walking Dead like two hours ago. Today I experienced the unholy treachery of figuring out what was wrong with my git. Simple solution was to install an SSH key, which worked, but then another issue reared its Medusa head: I had initialized a parent directory instead of the individual directories for each of my projects. So, with the help of Ashley, I de-initialized the parent and initialized the child. The good news was the staging and committing and pushing was all kosher. 

The rest of the day I made a bit of progress on Ruby, and reviewed everything I’d learned thus far re: basics, control flow, and loops & iterators. I also made flashcards of my fav examples from treehouse and code academy. Flash cards are time consuming, but they make me feel grounded. I can’t memorize this stuff without them, and I think that memorization will pay off when I’m writing more abstract code and don’t get distracted by looking up simple syntax

Tomorrow I will conquer Ruby hashes and symbols, make more flash cards, and review everything else Ruby again.

Day 1

Hello world, or should I say “farewell,” seeing as I’ll not be seeing much of it for the next few months. I’m okay with that. Really. I’m an introvert at heart, and one of the reasons I chose to become a developer is that it’s a suitable career for my ilk. I know, I know. The world has gone extrovert. I got the memo. I just think the font is too loud.

With that said, I actually liked the toss the ball exercise in class today? Yes. So there must be a kernel of truth to today’s zeitgeist (although I did have a minor panic attack that I would drop the ball like I used to in grade school ALL the time). Whatever. In short, the game is a great tool for getting people like me to interact in class.

I also decided to become a developer because I love to learn, and today I learned a lot! I started with some git review re: staging, committing and pushing, and I think I got the hang of it. Yay.

After, I moved on to Ruby, using Code Academy in conjunction with Treehouse so I don’t get bored. I covered the absolute basics and then moved on to control flow and loops (which i’m in the middle of). I pushed a lot of the work I did today to GitHub.

I still need to go back and work on the Skillshare website tutorial at some point. I’m super stoked about the git tutorial tomorrow. Great stuff.

Today’s notes:

  • irb
  • REPL – read evaluate print loop
  • subl hello.rb – sublime shortcut on command line
  • git add . — stages all files form current directory
  • Question: How do i avoid having to type my password every time i push to github?
  • everything in ruby is an object
  • a string is an object
  • you call a method on an object via .something
  • .length
  • ‘Christopher Haack’.reverse
  • puts ‘christopher haack’.upcase
  • puts ‘Christopher Haack’.downcase
  • # this is a comment
  • =begin
  • here is another demo
  • of a comment that spans two lines
  • =end
  • local variables start with lowercase and separated by underscores
  • first_name=’Chris’
  • string interpolation and replace the #{monkey} bit with the value of monkey
  • .capitalize! capitalizes the string in-place instead of making a copy. a lot of methods use !, but beware
  • until is the opposite of while