I'm studying how the Camping
web framework works right now, and I don't understand what the Camping::Server.start
at line 10 in /bin/camping
is doing.
I expected this to call the start
method in /lib/camping/server.rb
at line 131, and so I put a simple puts 'hello'
statement at the beginning of that method, expecting that statement to be invoked when I ran /bin/camping
. However, I never saw my puts statement get called, so I can only assume that it's not that start
method getting called.
I feel like I'm missing something obvious here. Here is the link to the camping github page and the relevant sections of code:
Github: https://github.com/camping/camping
From /bin/camping
:
#!/usr/bin/env ruby
$:.unshift File.dirname(__FILE__) + "/../lib"
require 'camping'
require 'camping/server'
begin
Camping::Server.start
rescue OptionParser::ParseError => ex
puts "did it error"
STDERR.puts "!! #{ex.message}"
puts "** use `#{File.basename($0)} --help` for more details..."
exit 1
end
From /lib/server.rb
:
def start
if options[:server] == "console"
puts "** Starting console"
@reloader.reload!
r = @reloader
eval("self", TOPLEVEL_BINDING).meta_def(:reload!) { r.reload!; nil }
ARGV.clear
IRB.start
exit
else
name = server.name[/\w+$/]
puts "** Starting #{name} on #{options[:Host]}:#{options[:Port]}"
super
end
end
My puts 'hello'
on Camping::Server.start
wasn't getting called because I didn't understand how static methods were defined in ruby.
start
was being called statically, and I realize now that the start
method I was looking at in the snippet wasn't a static method, which meant that another start
method was getting called. I looked into Camping::Server
and realized that it inherited from Rack::Server
, which has the following method:
def self.start(options = nil)
new(options).start
end
That was the method getting called, not the one on /lib/camping/server.rb
. I had been looking at the wrong method.