Loading...
Menu

Ruby on Rails Chapter 4: Rails Flavored Ruby

Home / Technology / Ruby on Rails Chapter 4: Rails Flavored Ruby

Ruby blog

This is the chapter where the real fun starts. Your introduction to the Rails Console.

Consider this console as the worry-free sandbox you used to play in as a child. If you built your mote wrong, you could simply kick sand back into it or walk away. With the Rails Console, if you get your code wrong, simply use Ctrl-C to break that last command or Ctrl-D to exit the console all together.

The console is great for new developers of all levels or developers new to Ruby. It allows you to test ideas and theories without the fear of permanent repercussions of damaging your precious database.

This comes in handy if you recursively delete your entire hard drive. (An action I will neither confirm nor deny happened while I was attending Transylvania)

Interpolation within a string.

When I wrote my first RoR program, I just jumped in. I started laying down the codes with no prior knowledge of Ruby or Rails. Turns out, that’s not the best approach with anything—especially completely new concepts which I’ve previously covered.

So, let’s start with interpolation. According to Merriam-Webster, interpolate means—to put (something) between other things or parts. But for our purposes, it means to display the associated variable within a string.

For example, if I put:

myVar = "Jerrod is awesome"

in the console and then typed:

"Who is awesome? {myVar}"

the output will be:

"Who is awesome? Jerrod is awesome"

Common programming mistake

A common error/misconception among new programmers is the difference between a double quote (“) and a single quote (‘). In most cases with Ruby, they behave exactly the same.

However, had I entered:

‘Who is awesome? {myVar}’

The output would have been

‘Who is awesome? {myVar}’

As you can see, this doesn’t do us any good. Some of you may even be asking how setting a variable and then outputting it is of any use to a programmer, anyway.

Usage case of interpolation

The practice of interpolating a variable within a string would be most handy when you receive input from the user. We have all experienced this on a site where you have signed up and once you logged in a personalized message is displayed, like:

"Welcome back, Jerrod."

Everyone would see “Welcome back, ” with the name variable changing.

This is only the tip of the iceberg when it comes to the capabilities of Ruby on Rails. After this chapter I felt comfortable enough in the language to stop going through the tutorial. You will see these lessons (and many more) in action, with the sites we’re creating here at Current360 using Ruby on Rails.

Comments(0)

Leave a Comment

eight − 3 =