2015-02-14 2 views
0

я добавил два метода класса Date и его поместили в lib/core_ext следующим образом:NoMethodError по методу добавлен в Date класс

class Date 
    def self.new_from_hash(hash) 
    Date.new flatten_date_array hash 
    end 

    private 
    def self.flatten_date_array(hash) 
    %w(1 2 3).map { |e| hash["date(#{e}i)"].to_i } 
    end 
end 

затем создал тест

require 'test_helper' 

class DateTest < ActiveSupport::TestCase 
    test 'the truth' do 
    assert true 
    end 

    test 'can create regular Date' do 
    date = Date.new 
    assert date.acts_like_date? 
    end 

    test 'date from hash acts like date' do 
    hash = ['1i' => 2015, '2i'=> 'February', '3i' => 14] 
    date = Date.new_from_hash hash 
    assert date.acts_like_date? 
    end 
end 

Теперь я получаю ошибка: Minitest::UnexpectedError: NoMethodError: undefined method 'flatten_date_array' for Date:Class

Я неправильно определил свой метод или что-то еще? Я даже попытался переместить метод flatten_date_array внутри new_from_hash и все еще получил ошибку. Я также попытался создать тест в MiniTest и получил ту же ошибку.

ответ

1

частный не работает для методов класса и использует self.

class Date 
    def self.new_from_hash(hash) 
    self.new self.flatten_date_array hash 
    end 

    def self.flatten_date_array(hash) 
    %w(1 2 3).map { |e| hash["date(#{e}i)"].to_i } 
    end 
end 
+0

Чтобы быть честным, я не знаю деталей, почему, я хочу дать комментарий, но у меня нет права. – jerrytao

Смежные вопросы