Ok. You asked a lot of questions in one. It will be hard to explain all ruby and rails magic in one answer but I'll try to give you some useful resources where you can find some answers.
1) About require statements. If you are new to ruby and rails it will be hard to understand how rails application is initialized. Here is a tutorial were you can some useful information:
If you need more information about particular method you can always look into documentation. For example redirect_to method info:
2) About "Using methods that don't exist". This is one of the most beautiful feature of ruby language called metaprogramming. This is also advanced topic. Here are some useful resources:
Ruby metaprogramming online tutorial
I don't understand your example. Are you generating the source for the method as well?
So I will start with an example from the book Perrotta: Metaprogramming Ruby
define_method :my_method do |my_arg|
my_arg * 3
obj = MyClass.new
obj.my_method(2) # => 6
instance = new
Why is this?
Well, in first place, you have to understand that you are calling a class method, thus you are at a class level already. The .new method is a class method, so you can call it directly without calling self.class.new.
Why does self.class.new return Class?
Because the class of the class Car is Class (I know, sounds weird ;), because classes in Ruby are instances of Class.
This is actually a pretty deep concept, I recommend you read more about it. One nice reference I have read is the book Metaprogramming Ruby by Paolo Perrotta (ISBN-10: 1934356476) .
root is defined on ActionDispatch::Routing::Mapper::Base, which is included by ActionDispatch::Routing::Mapper.
There is a little more going on behind the scenes than at first glance.
If we look at the definition of draw, we can see why:
The block is converted to a proc (with &) and passed to eval_block which is defined a bit farther down:
mapper = Mapper.new(self)
So, as it turns out, yield isn't used at all. The block we pass to Application.routes.draw is just being evaluated in the context of an instance of ActionDispatch::Routing::Mapper.
If we add puts self to our config/routes.rb, we can see this in action:
When we run rake routes we get something similar to the following:
For further reading, see:
"Metaprogramming Ruby" is well worth adding to your bookshelf.
def make_class(method_name, method_body, s_value)
define_method method_name do |*args|
define_method :to_s do
"Metaprogramming Ruby: Program Like the Ruby Pros" is a very good book to learn about ruby metaprogramming.
First read the article in Wikipedia about self (even if it does not mention Ruby at all).
To make a long story short:
If you have get all that, take a look at Metaprogramming Ruby which tells you some more tricks how to use self, classes, eigenclasses and some other interesting things.
In Ruby local variables only accessible in the scope that they are defined. Whenever you enter/leave a Class, a Module or a Method definiton your scope changes in Ruby.
For instance :
v1 = 1
class MyClass # SCOPE GATE: entering class
v2 = 2
local_variables # => ["v2"]
def my_method # SCOPE GATE: entering def
v3 = 3
local_variables # => ["v3"]
end # SCOPE GATE: leaving def
local_variables # => ["v2"]
end # SCOPE GATE: leaving class
These entering and leaving points are called Scope Gates. Since you enter through Scope Gate via method definition you cannot access your local_var inside hello method.
You can use Scope Flattening concept the pass your variable through these gates.
For instance instead of using def for defining your method you can use Module#define_method.
local_var = "Hello"
define_method :hello do
In the same way you can define your classes via Class#New so that your scope does not change when you pass through class definition.
local_var = 'test'
MyClass = Class.new do
puts local_var #valid
puts local_var #invalid
In the same way you should use Module#New if you want to pass your local variables through Module gates.
Example is taken from Metaprogramming Ruby