Last active
May 27, 2019 14:13
-
-
Save sunmockyang/1d679f9a46b02aae919e3df4f418850b to your computer and use it in GitHub Desktop.
A ruby template class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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