1

Wondering if anyone can see why I cant call .modulo. I feel like its something like I have to call Math in the class but I Im not sure thats the case.

git: https://github.com/jbasalone/ja/tree/master/ruby/exercism.io/leap

the error:

test_leap_year(YearTest):
    NoMethodError: undefined method `modulo' for nil:NilClass
    /Users/jennyb/ja/ja/ruby/exercism.io/leap/year.rb:26:in `divbyfour?'
    /Users/jennyb/ja/ja/ruby/exercism.io/leap/year.rb:17:in `leap?'
    leap_test.rb:6:in `test_leap_year'

This is my code:

class Year

    def initialize(theyear)
        @theyear = theyear
    end
class << self

    attr_reader :theyear

    def initialize(theyear)
        @theyear = theyear
    end

    def leap?(theyear)
        divbyfour? && !acentury? || bcentury?
    end
    #private

    def acentury?
        theyear.modulo 100 == 0
    end

    def divbyfour?
        theyear.modulo 4 == 0
    end

    def bcentury?
        theyear.modulo 400 == 0 
    end
    end
    end

And this is the test I run against it:

require 'minitest/autorun'
require_relative 'year'

class YearTest < MiniTest::Unit::TestCase
   def test_leap_year
     assert Year.leap?(1996)
   end

  def test_non_leap_year
    skip
    refute Year.leap?(1997)
  end

  def test_non_leap_even_year
    skip
    refute Year.leap?(1998)
  end

  def test_century
    skip
    refute Year.leap?(1900)
  end

  def test_fourth_century
    skip
    assert Year.leap?(2400)
  end
end
  • You don't initialize the Year class: Year.new(1996).leap? also you don't need arguments for leap? method: def leap?(theyear) divbyfour? && !acentury? || bcentury? end should be changed to def leap? divbyfour? && !acentury? || bcentury? end – Giannis Mar 30 '14 at 22:51
  • if I remove the the arguement I get; test_leap_year(YearTest): ArgumentError: wrong number of arguments (1 for 0) /Users/jennyb/code/training/ruby/leap/year.rb:21:in `leap?' leap_test.rb:6:in `test_leap_year' – Jennifer Basalone Mar 30 '14 at 22:53
  • Sorry my comment wasn't too clear, I posted an answer below, that worked for me. Hope it helps! – Giannis Mar 30 '14 at 23:18

2 Answers2

0

In divbyfour method you call modulo on theyear variable which doesn't exist.

Here's a code that fixes your problem.

class Year
  attr_reader :theyear

  def initialize(theyear)
    @theyear = theyear
  end

  def leap?
    divbyfour? && !acentury? || bcentury?
  end

  def acentury?
    theyear.modulo(100) == 0
  end

  def divbyfour?
    theyear.modulo(4) == 0
  end

  def bcentury?
    theyear.modulo(400) == 0
  end
end

Example:

>> year = Year.new 2014
=> #<Year:0xb8eb15b4 @theyear=2014>
>> year.leap?
=> false

What I did:

If you have time, check out Ruby Koans – it's a great way to learn Ruby and its idioms.

Community
  • 1
  • 1
Rafał Cieślak
  • 972
  • 1
  • 8
  • 25
0

This works for me:

class Year

    attr_accessor :theyear

    def initialize(theyear)
        @theyear = theyear
    end

    def leap?
        divbyfour? && !acentury? || bcentury?
    end
    #private

    def acentury?
        theyear.modulo(100) == 0
    end

    def divbyfour?
        theyear.modulo(4) == 0
    end

    def bcentury?
        theyear.modulo(400) == 0 
    end
end

And then you call it with:

y = Year.new(1995)
y.leap?

Alternatively you can use it like this:

class Year

    def self.leap?(year)
        @theyear = year
        divbyfour? && !acentury? || bcentury?
    end

    def self.theyear
      @theyear
    end
    #private

    def self.acentury?
        theyear.modulo(100) == 0
    end

    def self.divbyfour?
        theyear.modulo(4) == 0
    end

    def self.bcentury?
        theyear.modulo(400) == 0 
    end
end

And then you don't need to initialize the Year class.

Year.leap?(1995)
=> false
Giannis
  • 813
  • 7
  • 13
  • yea youd think that would work. Thats how I had it originally but the test expects a class method – Jennifer Basalone Mar 30 '14 at 23:15
  • Nice. It brought on something new but it progressed. 1) Error: test_leap_year(YearTest): TypeError: false can't be coerced into Fixnum /Users/jennyb/code/training/ruby/leap/year.rb:23:in `modulo' /Users/jennyb/code/training/ruby/leap/year.rb:23:in `divbyfour?' /Users/jennyb/code/training/ruby/leap/year.rb:10:in `leap?' leap_test.rb:6:in `test_leap_year' – Jennifer Basalone Mar 30 '14 at 23:30
  • You probably didn't change the modulo method like this: theyear.modulo(4) == 0 It needs the parentheses in order to work. The error you receive is the one I got when I tried it without parentheses. – Giannis Mar 31 '14 at 07:17
  • I used theyear.modulo(100).zero? cause I thought it was sexier. Thanks! – Jennifer Basalone Mar 31 '14 at 18:38
  • Hehe nice :-) Glad I was of help! – Giannis Apr 01 '14 at 09:33