DEV Community

Cover image for Walking in unfamiliar shoes: Ruby feels weird to me.

Walking in unfamiliar shoes: Ruby feels weird to me.

Cover image credit: Chris Evans

I've been using Python for years, but I recently wrote my first bit of ruby. There are many things that felt weird to me, which I guess is normal when you're writing in a language that's new to you.

I'm going to start with the weirdest thing to me

No methods for function method calls

In python, the expression foo.bar means "look in the current scope and any outer scopes for a thing named foo, then, get an attribute named bar from it."

In ruby, what this expression seems to mean, as far as I understand (please let me know whether my mental model is right) "Look up a thing called foo in the current scope or any outer scopes. If it's a method, call it with no arguments, directly use the value. On the result, look for a method called bar, and call it with no arguments."

So for example: gets.chomp calls the method gets, and calls chomp on the result. (in ruby, this gets a line of input from stdin, and then removes trailing whitespace.)

In Python, to get the same result, if the functions had the same name, you'd do: gets().chomp()

It often felt to me like ruby would need a lot less syntactic sugar if they just required brackets for method calls. For example, you don't need an @ for instance fields if you can just access them with this.field. There would be no need for the instance.method(:foo)method to access a method, since you just access it by refering to its name without adding brackets (i.e. instance.foo)

But in the end, I guess it just feels weird to me that referring to a variable and calling a method with no arguments have exactly the same syntax.

Some smaller things

The way you end blocks with end felt weird at the start, but I actually really like this. Most editors automatically indent code blocks anyway, so there's no need to enforce it in the language like Python does. What I like about ruby's end is that it is clear how many code blocks you're closing. If you have long blocks of indented code (yes I know you shouldn't), it's often hard in Python to see which code block you're closing.

Another thing I found strange is that require doesn't load things into their own namespace. If you import something in python, anything in the something module will be available under the something namespace. Most ruby programs seem to fix this by putting things in a module declaration, but having to syntactically declare this feels weird. I do like the distinction between load and require though.

Re-opening classes was another interesting tidbit I found. If you do

class A
    def foo
        # do stuff
    end
end

class A
    def bar
        # do stuff
    end
end


`

After this code, class A will have a foo and a bar method. I found this useful when toying around in the interactive ruby shell, but I imagine this could get confusing in larger ruby projects, since there's nothing in this class declaration telling me that there's another part of this class written somewhere else.

I'm not sure I like implicit returns. Growing up a pythonista, I've always believed that explicit is better than implicit.

I still need to completely wrap my head around blocks, Procs and lambda's.

So that wraps it up.

Top comments (6)

Collapse
 
lodestone profile image
Matt Petty

I get what you are saying here, but in the inverse.

I'm a long time Rubyist and every time I look at python code with all the "missing" end keywords, I get this feeling like I am looking at code with no pants on.

Collapse
 
austinthecoder profile image
Austin Schneider

Although re-opening classes is allowed, it's mostly avoided and probably considered bad form. That's the thing with Ruby, you can "do anything" (like access private methods with send), but not that you should.

Collapse
 
georgeoffley profile image
George Offley

Ruby seems almost counter intuitive to me. Also coming from a Python background, it seems like using your left had when you've been right handed all your life.

Collapse
 
_bigblind profile image
Frederik 👨‍💻➡️🌐 Creemers

I had the exact same feeling at times.

Collapse
 
tunabee profile image
Steven LeBeau

As another Python person interested in Ruby, may I ask: what got you curious about learning Ruby? Do you plan to build web apps with it, or want to learn more about OO?

Collapse
 
_bigblind profile image
Frederik 👨‍💻➡️🌐 Creemers

Someone asked me to help out on a Ruby project. Also, there are several open source projects out there that I'd love to contribute to, but they're rails, so maybe I'll get myself to learn rails some day, but Django works well enough for what I want to do.