Class Method Error

I’m executing some tests on crystal interactive with one module and struct and have error calling one class method as below:

crystal i
Crystal interpreter 1.16.3 (2025-05-12).
EXPERIMENTAL SOFTWARE: if you find a bug, please consider opening an issue in
https://github.com/crystal-lang/crystal/issues/new/
icr:1>  module Space
icr:2>    struct Age
icr:3>      ORBITALS = {
icr:4>        "Mercury" => 0.2408467,
icr:5>        "Venus"   => 0.61519726,
icr:6>        "Earth"   => 1.0,
icr:7>        "Mars"    => 1.8808158,
icr:8>        "Jupiter" => 11.862615,
icr:9>        "Saturn"  => 29.447498,
icr:10>       "Uranus"  => 84.016846,
icr:11>       "Neptune" => 164.79132,
icr:12>     }
icr:13> 
icr:14>     EARTH_YEAR = 31_557_600
icr:15>     @@age : Int64 = 0
icr:16> 
icr:17>     def self.from_seconds(sec : Int64)
icr:18>       @@age = sec
icr:19>     end
icr:20> 
icr:21>     def self.age_on_earth
icr:22>       (@@age/EARTH_YEAR).round(2)
icr:23>     end
icr:24> 
icr:25>     def self.age_on_mercury
icr:26>       (@@age/EARTH_YEAR/ORBITALS["Mercury"]).round(2)
icr:27>     end
icr:28> 
icr:29>     def self.age_on_venus
icr:30>       (@@age/EARTH_YEAR/ORBITALS["Venus"]).round(2)
icr:31>     end
icr:32> 
icr:33>     def self.age_on_mars
icr:34>       (@@age/EARTH_YEAR/ORBITALS["Mars"]).round(2)
icr:35>     end
icr:36> 
icr:37>     def self.age_on_jupiter
icr:38>       (@@age/EARTH_YEAR/ORBITALS["Jupiter"]).round(2)
icr:39>     end
icr:40> 
icr:41>     def self.age_on_saturn
icr:42>       (@@age/EARTH_YEAR/ORBITALS["Neptune"]).round(2)
icr:43>     end
icr:44>   end
icr:45> end
 => nil
icr:46> Space::Age.from_seconds(1000000000).age_on_earth
error in line 1
Error: undefined method 'age_on_earth' for Int64

Please I need help because don’t understand that error. To me nothing incorrect on module and class.

Your Space::Age.from_seconds method returns an Int64, for which there is no #age_on_earth method, as the error says. Ultimately your root problem here is you’re treating Space::Age as a module, not as an actual instantiable type.

You probably meant to do something like:

module Space
  struct Age
    ORBITALS = {
      "Mercury" => 0.2408467,
      "Venus"   => 0.61519726,
      "Earth"   => 1.0,
      "Mars"    => 1.8808158,
      "Jupiter" => 11.862615,
      "Saturn"  => 29.447498,
      "Uranus"  => 84.016846,
      "Neptune" => 164.79132,
    }

    EARTH_YEAR = 31_557_600

    def self.from_seconds(seconds : Int64) : self
      new seconds
    end

    @age : Int64 = 0

    def initialize(@age : Int64); end

    def age_on_earth
      (@age/EARTH_YEAR).round(2)
    end

    def age_on_mercury
      (@age/EARTH_YEAR/ORBITALS["Mercury"]).round(2)
    end

    def age_on_venus
      (@age/EARTH_YEAR/ORBITALS["Venus"]).round(2)
    end

    def age_on_mars
      (@age/EARTH_YEAR/ORBITALS["Mars"]).round(2)
    end

    def age_on_jupiter
      (@age/EARTH_YEAR/ORBITALS["Jupiter"]).round(2)
    end

    def age_on_saturn
      (@age/EARTH_YEAR/ORBITALS["Neptune"]).round(2)
    end
  end
end

pp Space::Age.from_seconds(1000000000).age_on_earth # => 31.69

@ Blacksmoke16, thank you for your explanation. I understood the problem.