Class 在 Sinatra 中表现异常
Class behaving strangely inside Sinatra
我正在使用 Sinatra V1.4.6 和 Ruby v2.2.2。我的目录结构是:
SinatraTest
bin
app.rb
lib
fix_month.rb
views
test_view.rb
我的 app.rb
文件是:
require 'sinatra'
require './lib/fix_month'
set :port, 8080
set :views, 'views'
get '/' do
@month = FixMonth.from_string('Jan 16')
erb :test_view
end
我的 test_view.erb
包含:
<html>
<title>Test Page</title>
<body>
<%= @month %>
</body>
</html>
最后,我的 fix_month.rb
包含:
class FixMonth < Fixnum
def initialize(mon)
super( mon )
end
def self.from_string( s )
FixMonth.new(s.to_i)
end
def to_s
"#{@@months_as_string[@mon]} #{year.to_s}"
end
end
当我访问该网站的主页时,代码到达 class 方法 FixMonth.from_string
然后出现以下错误:
2015-11-01 20:34:04 - NoMethodError - undefined method `new' for FixMonth:Class:
我需要什么才能像其他任何东西一样使用我的 FixMonth class Ruby class?
What do I need to be able to use my FixMonth class like any other Ruby class?
不从没有 new
方法的 class 继承。
Sinatra 在这里是转移注意力,完全无关紧要。 Fixnum
s 是立即数,Fixnum
没有 new
方法。
我正在使用 Sinatra V1.4.6 和 Ruby v2.2.2。我的目录结构是:
SinatraTest
bin
app.rb
lib
fix_month.rb
views
test_view.rb
我的 app.rb
文件是:
require 'sinatra'
require './lib/fix_month'
set :port, 8080
set :views, 'views'
get '/' do
@month = FixMonth.from_string('Jan 16')
erb :test_view
end
我的 test_view.erb
包含:
<html>
<title>Test Page</title>
<body>
<%= @month %>
</body>
</html>
最后,我的 fix_month.rb
包含:
class FixMonth < Fixnum
def initialize(mon)
super( mon )
end
def self.from_string( s )
FixMonth.new(s.to_i)
end
def to_s
"#{@@months_as_string[@mon]} #{year.to_s}"
end
end
当我访问该网站的主页时,代码到达 class 方法 FixMonth.from_string
然后出现以下错误:
2015-11-01 20:34:04 - NoMethodError - undefined method `new' for FixMonth:Class:
我需要什么才能像其他任何东西一样使用我的 FixMonth class Ruby class?
What do I need to be able to use my FixMonth class like any other Ruby class?
不从没有 new
方法的 class 继承。
Sinatra 在这里是转移注意力,完全无关紧要。 Fixnum
s 是立即数,Fixnum
没有 new
方法。