Scoping Out Method Scopes

I have finally returned to Ruby and none too soon! Today, lets look at scopes in Ruby.

The scope of a program means that the variables we create do not exist everywhere in a program. This helps in preventing the programmer from overwriting a variable created by someone else (or, let’s be honest, themselves) in other parts of a program. I mean, it could still happen, but it reduces the risk.

Methods have their own scope. A variable created outside a method is unavailable to the method. This means if we set a variable outside of a method, and then create a method with variables inside that method, the variables outside the method are not accessible inside the method and vice versa. Let’s look at an example:

Now, in the above I have defined a variable outside of the method, my_name, and set it equal to Nick. When I run the Ruby file I get the immediate response below the defined method, #greeting, that puts the my_name variable to the screen. It also returns nil. Below that I call the #greeting method and pass in the argument “Sophie” which runs and spits out an error.

See here:

So, we see that outside the method that my_name variable is accessible. Inside the method, however, my_name is inaccessible. Let’s try something a little different. What if I have this?

What will happen? If you run the file, my_name variable will be puts to the screen and then we get an error. See here:

At this point the program stops, thus #greeting method is never called. Why? Because inside_voice is a method scope variable and inaccessible outside of it.

I Overestimated!

OK, so good news, bad news here…

The good news is I’m still making progress. The bad news is I’m still in HTML/ CSS. I’m in the last section, and I’m a quarter of the way through it, but my progress has been a little slower than I thought. I blame being a little under the weather and my not liking CSS in general. I can see Ruby from here, however.

Still Trudging Through CSS

I need this coffee mug:

I’m almost through all of this, and it reminds me why I really don’t want to work on front end. I don’t feel that design oriented bug, and I really get upset when CSS just doesn’t work at times. I also struggle with identifying slight differences in shades of gray, apparently. Moving forward!

Making Progress Again

I promise once through the HTML and CSS stuff I will get back to a bit more substantial posts. Since I am a completist, I want to do these. So far, I can at least say these are a good refresher, and I’ve learned a few things. Most is pretty familiar, however…

Delayed Due to a Software Problem

Well, despite coming home with time to work on HTML and CSS, I ran into a snag with the Learn IDE provided by Flatiron. After struggling with some sort of crazy ssh issue, I decided to take the advice of a fellow student and setup my system to use Sublime Text manually with the Learn curriculum. I am extremely happy I did so. I like Atom, but I missed Sublime Text! If you wish to do the same, go here.

Almost Completed a Full Month at Flatiron School

Here is a snapshot of where I am so far at Flatiron School. Note, the Learning Streak box seems bugged. The little “Longest Streak” continues to tick up, but Current all of a sudden went to zero one day. I have completed, as of today, 28 straight days!

My current goal is to get through the HTML and CSS portion as fast as possible since I’ve completed these elsewhere multiple times. It just bothers me to skip stuff.

Quick Update

I just finished up the Git section of the Flatiron curriculum and I’m moving into HTML and CSS. Hoping to get through it fairly quickly since I’ve worked on this before in several tutorials and such. You never know, however, if there is something new or interesting in store. After this its back into some Ruby!

I’m also going to start preparing for my talk on Bmore on Rails here soon. More details forthcoming!