Walking in unfamiliar shoes: Ruby feels weird to me.

Frederik 👨‍💻➡️🌐 Creemers - Mar 22 '17 - - Dev Community

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player