I am learning
name = request.fullpath.split('/').second.downcase
if name == '~' then name = name[1..-1] end
ns = Namespace.where(name_lower: request.fullpath.split('/').second.downcase).first
get ':namespace' => 'namespaces#show'
In Ruby you can use far more characters then in most other languages when naming methods.
Among those are
!. They have no special meaning to the interpreter.
However the convention in the community is that methods ending in
? are interrogative. They tell you if something is true or false.
class Person attr_accessor :age def initialize(age = 0) @age = age end def drinking_age? @age >= 18 end end
This request var wasn't defined，is rails creates it?
request in this context is a method argument.
.matches? method the local variable
request is whatever you have passed into the method.
Rails calls something like
NamespaceConstraint.matches?(request)* when it checks if the incoming request matches your custom constraint.
request object is created by the Rack middleware.
not is keyword that negates the following expression. Just like in english.
! is more commonly used due to precedence.
nil in ruby is nothing - a value that is not defined or has no value.
.nil? tells you if a variable is nil. Every object in ruby responds to this method.
irb(main):007:0> 0.nil? => false irb(main):008:0> false.nil? => false irb(main):009:0> nil.nil? => true
not ns.nil? translates to plain english as:
is ns not nothing? or
is ns anything?.
You're really out of you're depth. The only reason you would do something like this is if you where building a multi-tenant app - which is a hardly a task suited for a beginner.
First learn the basics of the Ruby language.
Then revisit Rails. Learning both a programming language and a framework at the same time is not a really good idea as you will mush both together mentally.