0

I am trying to output "59, bien sur", by modifying age method in class FrancePresident. I need to do this using #{catchphrase}. I have tried many ways including calling self.age . See Rspec below code.

module Presidential
  attr_accessor :name, :age, :citizenship

  def initialize(name, age)
    @name, @age, @citizenship = name, age, self.class.citizenship
  end

end

class UnitedStatesPresident 
  include Presidential
   def self.citizenship
     "The United States of America"
   end
 end

class FrancePresident 
 include Presidential
 def name 
   name + ", #{catchphrase}"
 end
 def age
 end
 def citizenship
  "#{self.class.citizenship}, #{catchphrase}"
 end
 def self.citizenship
   "La France"
 end
 def catchphrase
   "bien sur"
 end
end

RSPEC
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

  describe "inheritance" do
    it "should not inherit from President" do
      expect( FrancePresident.superclass.to_s ).not_to eq('President')
    end
  end
end
2
  • You can't create instances of a module(a module has no new() method), so defining initialize() and instance variables is WRONG. Commented Jun 25, 2015 at 4:39
  • I dont think I created an instance of the module. The module name is "Presidential" Commented Jun 25, 2015 at 4:44

2 Answers 2

1

And, this:

 def name 
   name + ", #{catchphrase}"
 end

results in unlimited recursion. name() is the getter, and inside name() you are calling name(). Inside a getter (and setter), you need to access the instance variables directly.

The following works:

require 'rspec/autorun' 

module Presidential
  attr_accessor :name, :age, :citizenship

  def initialize(name, age)
    @name, @age, @citizenship = name, age, self.class.citizenship
  end
end



class UnitedStatesPresident 
  include Presidential

  def self.citizenship
    "The United States of America"
  end
end



class FrancePresident 
  include Presidential

  def name 
   "#{@name}, #{catchphrase}"
  end

  def age
    "#{@age}, #{catchphrase}"
  end

  def citizenship
    "#{self.class.citizenship}, #{catchphrase}"
  end

  def self.citizenship
    "La France"
  end

  def catchphrase
    "bien sur"
  end
end


RSpec.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

  describe "inheritance" do
    it "should not inherit from President" do
      expect( FrancePresident.superclass.to_s ).not_to eq('President')
    end
  end
end

--output:--
$ ruby rspec_regular_ruby_code.rb 

Finished in 0.00159 seconds (files took 0.09554 seconds to load)
4 examples, 0 failures
Sign up to request clarification or add additional context in comments.

Comments

0

Change age method to like this,

def age
  "#{@age}, #{catchphrase}"
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.