Skip to content

Instantly share code, notes, and snippets.

@sunmockyang
Last active May 27, 2019 14:13
Show Gist options
  • Select an option

  • Save sunmockyang/1d679f9a46b02aae919e3df4f418850b to your computer and use it in GitHub Desktop.

Select an option

Save sunmockyang/1d679f9a46b02aae919e3df4f418850b to your computer and use it in GitHub Desktop.
A ruby template class
class RubyClass
attr_accessor :name, :size # Creates getter and setter methods.
@@class_variable = 0
# constructor
def initialize(name, size)
@name = name
@size = size
@@class_variable += 1
end
def self.class_function()
puts "class function"
end
def member_function(args)
puts "Hello #{args}!"
end
def to_s
"Hi my name is #{name}, and I am size #{size}"
end
end
# creating object
obj = RubyClass.new("Jose", "30")
# calling method using object
obj.member_function("Sunmock")
puts obj.name # => Jose
puts obj.size # => 30
obj.size = 40
puts obj.size # => 40
puts RubyClass.class_function # => class function
puts RubyClass.class_variable # => 1
puts obj #=> "Hi my name is Jose, and I am size 40"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment