schow
schow

Reputation: 33

Class Method in Ruby

I am new to Ruby and was trying out my first few programs in Ruby to understand the concepts. Now, in Class Method concept while trying out the basics I came across the following problem.

I have a Class method "Servers.valid_requestor".

This is supposed to check if the supplied username is valid one based on a pre-defined username I'm using and if yes it should execute certain piece of code in main.

Now the issue here is whenever I try to get the username by using myInput.user_name it returns Undefined method user_name for the my_input object (NoMethodError)

in `validRequestor': undefined method `user_name' for #Object (NoMethodError)
    class Servers
      # Constructor
      def initialize(user_name, ip_address, host_name)
        @user_name = user_name
        @ip_address = ip_address
        @host_name = host_name
      end

      # Class Method
      def Servers.valid_requestor(my_input)
        user_name = my_input.user_name
        return user_name.to_s == "myUserName"
      end 

      # Member function
      def print
        puts "I am in super"
        my_details = "Details: #{@user_name} -- #{@ip_address} -- #{@host_name}"
      end
    end 

    # MAIN
    a_server = Servers.new("myUserName", "10.20.30.40", "server1.mydomain.private")

    if (Servers.valid_requestor(a_server))
      my_details = a_server.print
      puts my_details
      <do something>
    end

If I add a method as below in Servers class, the issue resolves as I believe myInput.userName looks for a method.

    def user_name
      @user_name
    end

But can I get some pointer on why I need to have this method. I am trying to understand how exactly this work internally when you call something like my_input.user_name

I tried to get some documentation in detail on Class methods but in most cases those are explained with very simple example like some puts statements. If anyone can explain Class Method and accessing variables, objects etc inside a class method it would be really helpful. I have seen answers similar to one mentioned in "undefined method" error ruby , that gives me the idea to resolve the issue, but I needed a bit more details on how it internally works. Any pointer will be helpful.

Upvotes: 0

Views: 187

Answers (1)

BroiSatse
BroiSatse

Reputation: 44715

Your problem has nothing to do with class method but with getting instance variables in the outer world.

All the instance variable (starting with @) are private to the instance. To make them visible to the outer world you need to define the public method which will return it, like the method you defined. In ruby each methods return the value of the last executed expression, hence no need for return statement here.

This getter methods can be defined as well by attr_reader :userName. If you rather prefer to define setter (without reader), you can do attr_setter :userName, which is the same as:

def userName=(value)
  @userName = value
end

And finally if you want both, you can do attr_accessor :userName.

One note: ruby naming convention - we are using snake case for methods and variables and large camel notation for constants. Hence it should be rather user_name than userName.

Upvotes: 0

Related Questions