Day 31 – First Day of Ruby

First impression: Ruby on Rails has a lot of convenient features and does a lot of stuff automatically, which great, but I’m glad I learned Python and Django first so that I know what’s going on behind the scenes. All in all I’m off to a good start!

Ruby

As soon as I started Ruby, I was surprised by how the syntax seemed so similar to Python, yet different in super weird ways. In both Python and Ruby, there are no curly brackets to denote blocks of code, and in Ruby even parentheses for functions are optional. Also Python and Ruby both have many keywords that resemble English. On the other hand, Python uses semi-colons and indentation to differentiate scope, but Ruby doesn’t care about any of that and instead just uses an “end” statement. For example, take a look at the following functions in Python and Ruby:

#Python...
def are_equal(a, b):
    if a == b:
        return True
    return False

#Ruby...
def are_equal a, b
    if a == b
        return true
    end
    false
end

As you can see, Ruby has no semi-colon! But it does have two “end” keywords, here, which makes it look more verbose than its Python equivalent. There is an “end” for the end of the function itself, and there is also an “end” for the end of the “if” statement. So far, I’m not a huge fan of the end keyword. It seems to me like it will be just as easy to forget and lose track of as JavaScript’s curly brackets, but at least when writing in JavaScript code editors will usually close your curlies for you!

Also notice how the last statement before the end of the function is just “false”, not “return false”. This works because Ruby automatically returns whatever value was last evaluated at the end of every function if no explicit return statement was triggered before that. If there is no specified return statement and no evaluated value to return, a Ruby function will return “nil” by default.

Someone who’s already worked in Ruby may be cringing after seeing how I wrote the Ruby function above. Don’t worry, I’m about to fix it! You see, since Ruby doesn’t care about semi-colons or indentation, there’s really no reason to write the function above in that “Python-y” way. Check this out:

#Ruby...
def are_equal a, b
    return true if a == b else false
end

Pretty cool huh? It’s so clean! Ruby makes it super easy to write just about everything in one line! I still can’t decide whether this will actually make my code more or less readable in the long run, but it’ll definitely make it easier to write! The way these one line statements are written actually reads pretty similarly to how one would describe a function in normal English(pseudo-code): “Return true if ‘a’ is equal to ‘b,’ otherwise return false.”

Rails

I just started getting into Rails by the end of the day today, so all I’ve done is play with models a bit. Still, I can tell that Rails is an awesome framework. With just a few lines of code I can start up a database and start populating it with users, and not just that, I can even have all the fields on my User model validated, complete with auto-generated error messages. Work that took hours the first couple times to set up in Django now takes under five minutes with Rails. All I can say is wow.

I’ll get more into Rails tomorrow. As for general happenings at the Dojo today, there were a couple other tidbits worth mentioning. Algorithm time this morning was about recursion. Recursion is pretty cool and I’ve used in the past without knowing what to call it. I’ll probably write another post about recursion later. Also, seeing as my cohort has just started the Ruby stack along with the cohort before us, we all merged together and squished into the far corner of the Dojo. Instructor David even had us get in a circle and do self-introductions again. Can’t really remember too many names from that activity, but I’m sure I’ll get to know all the other cohort people soon enough. Hooray for networking!

(note: featured image found at https://careerfoundry.com/en/blog/web-development/10-great-sites-built-with-ruby-on-rails/)
Advertisements

2 thoughts on “Day 31 – First Day of Ruby

Leave a Reply

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