samedi 27 juin 2015

ruby: calling a instance method without using instance

I know in ruby, when we call an instance method, we need to firstly instantiate a class object. But when I see a open sourced code I got confused. The code is like this:

File Message.rb
require 'json'

module Yora
  module Message
    def serialize(msg)
      JSON.generate(msg)
    end

    def deserialize(raw, symbolized_key = true)
      msg = JSON.parse(raw, create_additions: true)
      if symbolized_key
        Hash[msg.map { |k, v| [k.to_sym, v] }]
      else
        msg
      end
    end
  end
end



File. Persistance.rb


require 'fileutils'
require_relative 'message'

module Yora
  module Persistence
    class SimpleFile
      include Message

      def initialize(node_id, node_address)
        @node_id, @node_address = node_id, node_address

        FileUtils.mkdir_p "data/#{node_id}"

        @log_path = "data/#{node_id}/log.txt"
        @metadata_path = "data/#{node_id}/metadata.txt"
        @snapshot_path = "data/#{node_id}/snapshot.txt"
      end

      def read_metadata
        metadata = {
          current_term: 0,
          voted_for: nil,
          cluster: { @node_id => @node_address }
        }
        if File.exist?(@metadata_path)
          metadata = deserialize(File.read(@metadata_path)) #<============
        end

        $stderr.puts "-- metadata = #{metadata}"

        metadata
      end

.....

You can see the line I marked with "<===" It uses deserialize function that been defined in message class. And from message class we can see that method is a instance method, not class method. So why can we call it without instantiating anything like this?

thanks

Aucun commentaire:

Enregistrer un commentaire