Coming from JavaScript to Ruby
Tags: javascript, ruby, beginners
Published: 2020-09-20 01:21:56

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][1] 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.
```ruby
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.
```ruby
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.
```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.
```ruby
my_car = {
  :color => "blue",
  :wheels => 4
}
```
However, there is an even shorter syntax that you can use when using symbols.
```ruby
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][2].

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



[1]:https://github.com/rvm/rvm
[2]:https://www.theodinproject.com/courses/ruby-programming