by Tronno · 3y ago · 0 comments
The "magic" (convention over configuration, autoloading, metaprogramming, etc) makes it difficult to know what methods are available to use. I end up over-relying on the official docs, which are of inconsistent quality. One example: api.rubyonrails.org lacks code examples, doesn't usually state what a given method returns, and many entries are outright blank.
Editor integration could help, but feels lackluster. With TypeScript in VS Code, hints come up as I write. I can hover anything to see its type, methods, and source code. With Rails, I can poorly approximate this behavior with a collection of half-functional community extensions, or I can buy and use the slow, bloated RubyMine IDE.
Rails enabled me to build my product, but my experience doesn't match the love I hear from other devs. I wish I knew how to make it click.
The Rails Guides [1] are quite good (and much better than the API docs, even for the same topics). The "Getting Started" guide, in particular, was my first intro to Rails, and it's what convinced me to go all in — I built my entire prototype by just following along the guide, seeing what they were doing with their toy app, and then trying to whatever similar thing I needed in my app.
Coming from Python, the magic was distasteful to me, too. Rails adds a ton more magic on top — so, eventually, when I set aside Rails and just spent time with Ruby (writing scripts or what not), I realized I loved Ruby the language much more than Rails the framework (and all its baggage).
Stripping Rails down to its API-only mode (though it was a bit rough around the edges last I tried) got me closer to the Ruby of it all. Sinatra turned out to be a delightful little framework (though sadly I never got a chance to use it much). And, even though I've now moved on from Rails, I still like Ruby as a go-to scripting language — it makes it fun and easy to throw together a quick shell script or some data analysis, and it's very friendly to a functional programming style.
I guess what I'm saying is: You're not alone. Many people in the Rails community agree that there's a ton of hidden magic, sometimes too much [2]. But underneath it all is a delightful programming language.
[1]: https://guides.rubyonrails.org/
[2]: The docs for the Rails routing layer, Journey, are my favorite example of this: https://github.com/rails/journey#label-SYNOPSIS-3A
I also wrote stupid code first in my own version, and copied + pasted to chatGPT, asked it to improve it, or rewrite in ruby on rails style. And wow!! The rewrote version even went with explanation.
Beside that, you can use Rails console to test.
The whole just fire up an interactive session or rails console is probably the last thing I want to do but probably the only way to diagnose or figure out how things are truly working. What monkey patches are in the mix, what libs override methods I set, etc.
For me, Ruby is one of those programming languages that obviously takes a different programming approach and mindset that I don’t have. I don’t think Ruby is bad but I know it isn’t the tool that fits my skill set.
However, many times ruby libraries will use dynamic programming and method_missing to define methods on the fly… this makes it really hard to figure out what methods are actually available.
For that reason, I try to never use method missing to define core functionality in ruby, and hate when other gems do it.
E.g.
def method_missing(name, \*args)
if name =~ /^has_/
has(name, *args)
end
super
end
def respond_to(name)
if name =~ /^has_/
return true
end
super
endThis won’t work even with respond_to being defined
some_object.methods - {}.methods # or some other "parent" class/object
This does set subtraction to remove all the "core" object methods from your some_object methods, so you can see just what's unique about that object. For example, comparing one of my services with a parent Service class it inherits from: irb(main):017:0> PaypalService.methods - Service.methods
=> [:capture_invoice_funds, :months_price, :client, :create_prepay_invoice, :order_info]
instead of listing out the 187 methods that PaypalService has that it inherits from Service and every other parent object up the line, which is usually a surprisingly long ancestor list in any Ruby on Rails class (even POROs!).There are a TON of useful methods like this that I'd recommend getting familiar with, which makes exploration and discovery of code incredibly fast and efficient. You can find them all with the same trick: by using `methods` on any method. :)
irb(main):043:0> PaypalService.method(:months_price).methods - {}.methods
=>
[:<<,
:>>,
:arity,
:curry,
:source_location,
:call,
:parameters,
:original_name,
:owner,
:receiver,
:super_method,
:name,
:unbind,
:comment,
:source]
Some of the more relevant ones: irb(main):037:0> PaypalService.method(:months_price).arity
=> 1
irb(main):038:0> puts PaypalService.method(:months_price).parameters
req
n_months
irb(main):039:0> puts PaypalService.method(:months_price).source
def self.months_price(n_months)
case n_months
when 1
9.00
when 3
24.00
when 6
48.00
when 12
84.00
else
raise "Invalid month prepay: #{n_months}"
end
endIf you haven’t tried it in a while, it’s quite good on Apple’s arm chips. Doesn’t feel slow at all, and the battery hit is negligible.