Things I Learn from Owning Rails Class
Teacher | Marc-André Cournoyer |
Link | Owning Rails |
I’ve participated Marc’s Owning Rails online class recently. AWESOME!
The class has two parts. Marc led us to build a minimal version of Rails on first day, Marc focused on the structure and design pattern behind Rails. Trully I think it’s a live version of Rebuilding Rails, maybe you can read it as a substitution. Second day is the excellent part, took us diving into the real Rails source. Marc gave us a clear clue on what each part do and how they work, and also some practical introductions on how and where to keep on digging after class.
Beside professional, I should also mention that Marc is a really kind and patient guy.
Thanks a lot, Marc.
Following are some specific questions I’ve noted on class.
Is yield
still available after passing &block
?
yes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Passing block directly will omit the block-to-proc process, it can be more efficient.
How to make bindings of block get understood in object, which differs the environment the block defines?
When you define the block, it simply grabs the bindings that are there at that moment, and then it carries those bindings along when you pass the block into a method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Use instance_eval
to eval the new bindings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Why using ::File
in config.ru
?
1 2 3 4 |
|
Actually the code above is defined in module Rack, where Rack::File
already exists.
How to use include
to construct a method chain?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
One way to solve is to use prepend
instead of include
, introduced by Ruby 2.0.
Considering the compatibility, Rails may not start to use it. Here is the Rails way to solve
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
How instance variables shared betweet controller and view?
One way is to use instance_varialbles
and instance_variable_set/get
, to passing instance varaibles defined in action to the view object.
The other way is passing binding
directly.
1 2 3 |
|
Truth about binding
-
Binding.new
doesn’t work, you can only callKernel.binding
orProc#binding
. -
You can only use
binding
witheval
.eval('', binding)
orbinding.eval('')
When we create custom middleware is it a good practise to add new keys and values to env variable to transfer it between middlewares?
[02:35] <wawka> macournoyer: When we create custom middleware is it a good practise to add new keys and values to env variable to transfer it between middlewares ?
[02:36] <macournoyer> Rack specs recommend namespacing everything you put in the env var
[02:36] <macournoyer> eg.: Rails will do env[“action_controller.request_id”] = “…”
[02:36] <macournoyer> “action_controller.” is the namespace
[02:36] <wawka> macournoyer: ok
How to check a gem’s dependency and dive into the source code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
How to get the source location of the method calling?
In Rails, guess the logic part(activerecord, activesupport..) based on the method function, then use bundle open
to dive in.
Ruby supports Method#source_location
, Proc#source_location
to provides some information, not accurate though. Use it like this
1 2 |
|
Other stuffs
-
Marc’s gitconfig
-
Don’t try to understand everything.
-
Read concern, callbacks, core_ext and other ActiveSupport parts as a start.
-
Nice word learnt, Baseics
-
Try
mount
a rack-based app in Rails.