3 ms·
Not code or a codebase - but reading Eloquent Ruby every morning on the DC Metro made the commute to work fly by in an instant and kinda blew my mind. I will tr
by whalesalad 4y ago
Not code or a codebase - but reading Eloquent Ruby every morning on the DC Metro made the commute to work fly by in an instant and kinda blew my mind. I will try to illustrate the stuff that blew my mind below:
Quick backstory: a feature of Ruby (that you tend to either love or hate) is that you do not need to use parentheses to call a function.
if you have a function like so
def prefix_hello(name)
"hello, #{name}!"
end
you can call it like this
prefix_hello "fred"
=> "hello, fred!"
After learning about some of the spiritual ancestors of Ruby (like Smalltalk), message passing, learning everything is just an object and function calls are just messages being sent to objects, a lot of things clicked immediately for me. It was kind of a mind blown moment.
Take the `+` (add) symbol in `5 + 3` - in essence, it's just a regular method that is literally caled `def +(number)`, which belongs to the int/number object.
In laymans terms this code is saying, hey object (number) '5', call your + method with '3' as the argument.
You can simulate this in an `irb` shell:
irb(main):001:0> x = 10
=> 10
irb(main):002:0> x.send(:+, 10)
=> 20
It's all right there! `objectA.send(methodName, arg, arg, arg)`
Then you realize that this is true of many languages, Python included (__add__)... but I do think Ruby does this in a particularly, ahem, eloquent way that really impressed me.
This moment in my career/learning was almost like a springboard/catalyst and unlocked a lot of additional learning down the road, like getting involved with Erlang and Lisp/Clojure.