3 ms·
I’m a long time Ruby and Rails developer. I know how the magic works from reading source code. A lot of stuff uses method_missing and define_method. There’s al
by kayodelycaon 4y ago
I’m a long time Ruby and Rails developer. I know how the magic works from reading source code.
A lot of stuff uses method_missing and define_method. There’s also instance_eval, which is used to create DSLs like rspec. Once you know what they do, stuff makes a lot more sense.
- WirelessGigabit 4y agodefine_method is so weird. Breaks searching through source code.
- jacobsenscott 4y agoRuby tells you where methods are defined - it is worth your time to learn how explore ruby code from the REPL - this is the ruby way. For example (I have pry installed, but you can do it without pry) [5] pry(main)> $ user.first_name From: /Users/.../.asdf/installs/ruby/2.7.7/lib/ruby/gems/2.7.0/gems/activerecord- 6.0.6/lib/active_record/attribute_methods/read.rb:15: Owner: User::GeneratedAttributeMethods Visibility: public Signature: first_name() Number of lines: 4 def #{temp_method_name} name = #{attr_name_expr} _read_attribute(name) { |n| missing_attribute(n, caller) } end Tells me exactly where this "magic" method is defined, and I can pop open that file and read all the source.
- neon_electro 4y agoCan you point to where this is documented? Thanks for sharing!
- jacobsenscott 4y agohttps://pry.github.io/ https://pry.github.io/ - also a lot of features from Pry have made it into the default IRB these days, but I still use pry. I don't know the equivalent commands in IRB.