Error when I make a class functions (not instance functions)

Hi, today, for some needs in my class, I tried to add few functions:

module ISM

    class Port

        record Port,
            name : String,
            url : String do
            include JSON::Serializable
        end

        property name : String
        property url : String

        def initialize(@name = String.new,@url = String.new)
        end

        def self.filePathPrefix : String
            return Ism.settings.rootPath+ISM::Default::Path::PortsDirectory
        end

        def self.directoryPathPrefix : String
            return Ism.settings.rootPath+ISM::Default::Path::SoftwaresDirectory
        end

        def self.exists(name : String) : Bool
            return File.exists?(self.filePathPrefix+name+".json")
        end

        def self.delete(name : String)
            File.delete(self.filePathPrefix+ARGV[2+Ism.debugLevel]+".json")
            FileUtils.rm_r(self.directoryPathPrefix+ARGV[2+Ism.debugLevel])
        end

        def filePath : String
            return self.filePathPrefix+@name+".json"
        end

        def directoryPath : String
            return self.directoryPathPrefix+@name
        end

        def loadPortFile
            port = Port.from_json(File.read(filePath))
      
            @name = port.name
            @url = port.url
        end

        def writePortFile
            port = Port.new(@name,@url)

            file = File.open(filePath,"w")
            port.to_json(file)
            file.close
        end

        def open : Bool
            Dir.mkdir_p(directoryPath)

            Process.run("git",  args: ["init"],
                                chdir: directoryPath)
            Process.run("git",  args: [ "remote",
                                        "add",
                                        "origin",
                                        port.url],
                                chdir: directoryPath)

            process = Process.new("git",args: [ "ls-remote"],
                                        chdir: directoryPath)
            process.wait

            if process.success?
                port.writePortFile
            else
                FileUtils.rm_r(directoryPath)
            end

            return process.success?
        end

        def synchronize : Process
            return Process.new("git",   args: ["pull","origin",Ism.portsSettings.targetVersion],
                                        chdir: directoryPath)
        end

    end

end

But I have this error:

zohran@alienware-m17-r3 ~/Documents/Programmation/ISM $ crystal build Main.cr -o ism
Showing last frame. Use --error-trace for full trace.

In ISM/Port.cr:35:25

 35 | return self.filePathPrefix+@name+".json"
                  ^-------------
Error: undefined method 'filePathPrefix' for ISM::Port

Is it normal ?

Yes, self in the context of your instance #filePath method refers to that particular instance of the class, not the class itself. You can use class vars from an instance method, but have to use self.class instead of self. Or could also use do like Port.filePathPrefix.

1 Like

Thanks a lot !

Looking at it again, prob would fit better as a constant?