cpt_peter
cpt_peter

Reputation: 429

Class behaving strangely inside Sinatra

I am using Sinatra V1.4.6 with Ruby v2.2.2. My directory structure is:

SinatraTest
    bin
       app.rb
    lib
       fix_month.rb
    views
       test_view.rb

My app.rb file is:

require 'sinatra'

require './lib/fix_month'

set :port, 8080
set :views, 'views'

get '/' do
  @month = FixMonth.from_string('Jan 16')
  erb :test_view
end

My test_view.erb contains:

<html>
    <title>Test Page</title>
    <body>
        <%= @month %>
    </body>
</html>

Finally, my fix_month.rb contains:

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

When I access the home page of the web site, the code gets as far as the class method FixMonth.from_string and then bombs with the following error:

2015-11-01 20:34:04 - NoMethodError - undefined method `new' for FixMonth:Class:

What do I need to be able to use my FixMonth class like any other Ruby class?

Upvotes: 0

Views: 18

Answers (1)

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369420

What do I need to be able to use my FixMonth class like any other Ruby class?

Not inherit from a class that has no new method.

Sinatra is a red herring here, and completely irrelevant. Fixnums are immediate values, Fixnum doesn't have a new method.

Upvotes: 2

Related Questions