Ruby类(超级)

时间:2015-05-07 20:43:16

标签: ruby-on-rails ruby rspec

我本周刚刚在Bloc开始上课。我已经和Ruby一起练习了大约3个月,但我终于陷入了困境。希望能得到一些帮助。

我坚持的部分属于超级

以下是简单格式规范:

  • President类应在初始化时接受nameage,并为这两个属性定义getter和setter。

  • FrancePresidentUnitedStatesPresident应继承自President

  • President的两个子类应定义citizenship类方法,并根据需要返回"La France""The United States of America"

  • President的所有实例也应该有citizenship个方法,这些方法会返回类'国籍。

  • FrancePresident", bien sur"name被要求时,age的所有实例都应在其回复中附加citizenship。对于超级

  • 来说这是个好例子

以下是我试图满足的RSpec规范:

describe President do
  describe "initialization" do
    it "takes a name and age, and delegates citizenship to the class default" do
      prez = President.new("George Washington", 283)
      expect( prez.name ).to eq("George Washington")
      expect( prez.age ).to eq(283)
    end
  end

  describe "children" do
    it "is the parent of FrancePresident and UnitedStatesPresident" do
      expect( FrancePresident.superclass ).to eq(President)
      expect( UnitedStatesPresident.superclass ).to eq(President)
    end
  end
end

describe FrancePresident do
  describe "catchphrase" do
    it "sounds just right" do
      expect( FrancePresident.citizenship ).to eq("La France")
      sarcozy = FrancePresident.new("Nicolas Sarkozy", 59)
      expect( sarcozy.citizenship ).to eq("La France, bien sur")
      expect( sarcozy.age ).to eq("59, bien sur")
      expect( sarcozy.name ).to eq("Nicolas Sarkozy, bien sur")
    end
  end
end

这是我目前的代码:

class President
  attr_accessor :name, :age

  def initialize(name, age)
    @name = name
    @age = age 
  end
end

class FrancePresident < President
  def self.citizenship
    "La France"
  end
end

class UnitedStatesPresident < President
  def self.citizenship
    "The United States of America"
  end
end

总而言之,我一直试图让citizenship类方法起作用。从说明中看,他们似乎希望我在citizenship中定义President类方法。但我不知道这样做是如何起作用并符合规格的,因为就我所见,你不能为每个孩子单独设定公民身份。

所以基本上我被困在第三个&amp;第四个目标。如果你有时间,请回复我。

4 个答案:

答案 0 :(得分:2)

在您的规范中,您期望有两种citizenship响应。一个有流行语,一个没有。所以你必须有两种不同的方法。一类方法,一个实例方法。它应该遵循这些方针:

class FrancePresident < President
  def self.citizenship
    "La France"
  end

  # instance method which adds the catchphrase
  def citizenship
    append_catchphrase(self.class.citizenship)
  end

  # here's your super
  def age
    append_catchphrase(super)
  end

  def catchphrase
    'bien sur'
  end

  private

  def append_catchphrase(val)
    [val, catchphrase].join(', ')
  end
end

答案 1 :(得分:1)

你非常接近,除了类方法之外,你只需要在子类中定义实例方法

class FrancePresident < President
  def self.citizenship
    "La France"
  end

  def citizenship
    "La France, bien sur"
  end
end

class UnitedStatesPresident < President
  def self.citizenship
    "The United States of America"
  end

  def citizenship
    "The United States of America"
  end
end

答案 2 :(得分:1)

首先,我认为公民身份方法应该是实例方法,您可以在您的总统类中定义并在子类中覆盖它。 对于你的最后一个目标,这是一个例子:

class FrancePresident < President
  # citizenship method
  def name
    super + ", bien sur"
  end 
  def age
   super + ", bien sur"
  end
end

答案 3 :(得分:-1)

首先你不是在寻找一个类方法,它应该是实例方法。 这是我理解的重构代码形式:

class President
  attr_accessor :name, :age

  def initialize(name, age)
    @name = name
    @age = age
  end

  def citizenship
    self.class.citizenship
  end

end

class FrancePresident < President
  END_STRING = ", bien sur"

  def self.citizenship
    "La France"
  end

  def age
    "#{@age}#{END_STRING}"
  end

  def name
    "#{@name}#{END_STRING}"
  end
  def citizenship
    "#{self.class.citizenship}#{END_STRING}"
  end
end

class UnitedStatesPresident < President
  def self.citizenship
    "The United States of America"
  end
end



aa = UnitedStatesPresident.new("Roosevelt", 35)
puts aa.citizenship
puts aa.name
puts aa.age
ff = FrancePresident.new("France", 35)
puts ff.citizenship
puts ff.name
puts ff.age