DEV Community

Cover image for Coming from JavaScript to Ruby
Daniel Troyano
Daniel Troyano

Posted on

Coming from JavaScript to Ruby

So let's say you're got a handle on how to create a Node.js server and you want to expand your horizons. You've been hearing a lot about Rails, a popular MVC framework, and want to give it a try. But it uses Ruby, a completely different language!

Let's take a quick look at Ruby to see if learning another language is as scary as it sounds, (pro tip, it's not).


I'm not going to go into how to install Ruby on your computer, if don't already have it, I would recommend installing Ruby Version Manager for the same reasons you've probably been using Node Version Manager.


Ok, so you have Ruby installed and you want to give it a whirl! Well just open up your terminal and run irb. This starts up Interactive Ruby! It's a way to test out and run Ruby code right in your terminal.

We're going to be writing everything in irb, but if you wanted to, you could also make a file called something like test.rb and put all the code in there and use ruby test.rb in your terminal to run it. I'll use comments to denote the results of the code. Notice Ruby uses # instead of //.

Ok, so first of all, you don't need to use semi colons anymore! Ruby doesn't need anything to end a line. While yes, technically, you can use them to chain multiple commands on one line you definitely don't need to use them to end a single line. Also in Ruby, variable declaration is super easy. There's no more var, let, or const. Instead. you just write name = "Daniel". And now the name variable is set to a string of Daniel.

So if we wanted to declare several variables and print them to the screen, kind of like a console log, we could do this.

name = "Daniel"
age = 34
puts "Hi my name is #{name} and my age is #{age}"
# Hi my name is Daniel and my age is 34

Notice I snuck in string interpolation there too! It looks pretty much just like JavaScript, except with a # instead of a $. But it's important to know that in Ruby it only works with double quotes, not single quotes or back ticks, like in JavaScript.


Ruby is a pure object oriented language, so every variable is a reference to an object. This means that there's no difference in the way that it handles what JavaScript would call Simple or Primitive DataTypes.

Let's take a look at an example of that.

my_dog = "washburne"
my_favorite_dog = my_dog
my_dog.upcase!
puts my_favorite_dog
# WASHBURNE

As you can see changing my_dog changed my_favorite_dog. Let's talk a little about how we did that, because this is a very cool convention in Ruby. If a built in method in Ruby returns a changed copy of a variable, then you can affix a ! to the end of it to have it change the original. Also if you see a method end in ? that means it is going to return a boolean.

These are good conventions for your own methods as well!


Another big change is that JavaScript has objects, but the equivalent in Ruby is a hash. Like objects, hashes are key value pairs but the keys can be anything. So this is a valid hash in Ruby.

my_car = {
  "color" => "blue",
  "wheels" => true,
  4 => nil
}

Yes, I know none of those values made sense. But I just wanted to show they are possible. Now, normally when you use hashes you're going to use symbols for the keys. This is because symbols are faster and immutable. Even though "Danny" == "Danny" is true, if you were to check "Danny".object_id == "Danny".object_id it would be false. This is because they are pointing to different objects.

But that isn't true for symbols. They look like :name and two symbols with the same characters are exactly the same. So :name.object_id == :name.object_id would be true. So you could declare a hash like this.

my_car = {
  :color => "blue",
  :wheels => 4
}

However, there is an even shorter syntax that you can use when using symbols.

my_car = {
  color: "blue",
  wheels: 4
}

I imagine that looks really familiar to anyone coming from JavaScript. But you do need to keep in mind that it is working a little different in Ruby. Something to note is that you can't use dot notation to access the information in that variable. To get the color you would need to write my_car[:color].


Hopefully this small glance at Ruby has given you the courage to check it out. There are tons of helpful guides out there to help you along your way. I would recommend The Odin Project.

ps. I didn't even mention that Ruby uses == to check equality, but I bet you picked up on that.

Top comments (3)

Collapse
 
leastbad profile image
leastbad

I'm really excited for your learning journey, Daniel!

There is a #rails channel on the StimulusReflex Discord where there's about 500 Rubyists who take turns answering questions. I like to think that we're highly welcoming to beginners.

discord.gg/XveN625

Collapse
 
ericchapman profile image
Eric The Coder

That channel is indeed incredible! I was having a problem and they where 3 senior developers trying to help me at the same time. We solve my problem in 5 mins. My co-worker were impress and ask me « Who are those guys? » A great experience :-)

Collapse
 
dtroyano86 profile image
Daniel Troyano

That's awesome! I'm definitely going to check them out!