Milan
Milan

Reputation: 1529

how to use class in ruby

One syntax I am not able to understand is like this:

test = heavy::Test.new_test()

test.setq(name)

Here test is an object and in some other file there is module heavy and inside heavy there is another module Test but after that what is this new_test()?

Upvotes: 1

Views: 10504

Answers (1)

Salil
Salil

Reputation: 47472

class Heavy

end

class Test < Heavy

  def self.new_test  #This is class method (Written as self.method_name or model_name.method_name)
    puts "Hello World"
  end

  def setq(name)    #this is instance method (Call on object of a class not as class method)
   puts "Hello "+name
  end
end
test = Heavy::Test.new_test() # print 'Hello World'
test.setq('Salil')  #print 'Hello Salil'

class/module name must be CONSTANT (1st letter should be capital) (i.e.heavy should be Heavy)

Upvotes: 2

Related Questions