Ignition: Week 2

Week 2 is well under way now. The first week was fairly easy and now we are beginning to introduce new concepts and methods into our learning which is both fun and frightening at the same time.

Here is one example of something I was struggling on for a bit but ultimately came to understand much better.

Syntax

This is so key and I cannot stress this enough. If you are looking at code, whether it is Ruby or Javascript or HTML/CSS, it doesn’t matter. If you are seeing nothing but gobbledy-gook than you need to go back to fundamentals. Let’s look at a specific example that was tripping me up last week and I’ll explain my thinking on it.

 

Method – (sprintf)

          We had to take multiple values (The example provided above was in dollars and cents) and add it up, then output to the console. Here’s the problem I was having though, you put the amounts in as an integer gets.chomp.to_i and you’re only dealing with whole numbers. If you want to go out 2 decimal places you need to call a method on your variable (In this example, I am calling it on a variable named knife_juggle).

This is where I was really confused with the sprintf method. I didn’t understand what the characters in the method STOOD for. Once I saw it though, it made perfect sense. So here we go, this is how you take your integer and send it out 2 decimal places or how ever many you like:

Let’s take a total : 11.87. We want to display it out to 2 decimal places. So we call sprintf on it. We use a puts statement and we want to display our variable knife_juggle so we use string interpolation – #{knife_juggle} but we can’t leave it like that because it will only display whole numbers! So let’s modify it, using sprintf. Note: This method takes in two arguments:

The 1st – The way we will modify our variable 

%.2f, This had me so confused!! WTF, I dont get it. But then it clicked, % is just a placeholder, nothing more. You see, I thought it was a modulo which really threw me off. But no, it’s basically saying, “There’s going to be a value here“.

The .2f means “Send this value our 2 decimal places and make sure you treat it as a float.” If it is not a float and instead an integer it will not go out 2 decimal places, only floats can do that!

The 2nd – The variable to be modified     

The second argument is going to be the variable that you want to have altered. If you don’t put this here than you won’t get anything outputting and you’ll be really confused so don’t forget!

REMEMBER!!

The entire thing needs to be wrapped up in #{}, because this is inside of a puts statement and you are wanting to print that variable.

CONCLUSION

I know it was simple, but made a big difference to me in understanding the SYNTAX of ruby, which if we are being honest is absolutely paramount. Without understanding what different characters mean in a given context or understanding how to order things, you will be unable to write proper functioning code.

Once I came to this realization, I began messing around with different methods and writing up junk code to practice on so I could get a better feel for how this stuff is used in Ruby.

Check out these resources:

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s