Case Statements

I am continuing along on learning Ruby. Today, it’s a dive into case statements!

So, case statements are used to run multiple conditions against one value. To create a case statements you need:

  1. a value
  2. one or more conditions to compare the value to
  3. code to run if the condition is met

Case statements are useful when you have have a list of possible conditions and you need your code to operate if the condition is met. Consider this:

I mean, the above works but it is needlessly complex. Flatiron called it “code smell,” a phrase I am completely unfamiliar with but makes sense. We can do this better with a case statement. Let’s take a look:

So, let’s examine what we did! First we create the value we are testing

Then adding in conditions:

In the above, the name passed in will be tested against the condition and then, in this last part, if it meets it it will execute that code.

And, as seen above, we can add an else at the end that will run when no case value is met!

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.