NoMethodError для метода, добавленного в класс Date

Я добавил два метода в класс Date и поместил их в lib/core_ext следующим образом:

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 и получил ту же ошибку.


person ThomYorkkke    schedule 14.02.2015    source источник


Ответы (1)


private не работает для методов класса и использует 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
person jerrytao    schedule 14.02.2015
comment
Честно говоря, я не знаю подробностей, почему, я хочу дать комментарий, но не имею права. - person jerrytao; 14.02.2015