Currently, i am on break from my 3 years of journey as a web/software developer. I love my domain that includes ugly code turning into beautiful web-pages or applications. Since i am free after a long time and have good mood. I decided to write some tips about my love Ruby and Ruby on Rails.
Oh, god i hate this blogger....(Ignore this).
lets get started. We will create a sample class named "ClassTester".
>> class ClassTester
| Const1 = "one"
| Const2 = "two"
| #class level variables
| @@c_var1 = "class variables"
| @@c_var2 = "class variables 2"
| #instance variables
| #Note i have used attr_accessor for this example to get and set values
| attr_accessor :inst_one, :inst_two
| #public methods
| def hello_public
| p "Hi, i would like to meet you call"
| #protected methods
| def pro_me
| p "I am protected. some one can't take me home"
| #this can't be accessed from outside but can be inherited
| #private Methods
| def private_method
| p "Hey, tell me who i am"
Note: In this class tester we have some of the components. Now we will start gathering INFO about the class if we have haven't written this class.
If you have only the name 'ClassTester' then do this
- Get to know about the class if by using '.class'
ClassTester.class => Class
Note: If you are wondering you can use is_a? to know that, then you are wrong. see below
ClassTester.is_a? Class => true
Good but see this
ClassTester.is_a? Module => true
And this one
ClassTester.is_a? Object => true
Its well and ok use .class or .class.name
- Get the class methods
ClassTester.methods false => [:class_m]
If you are wondering what is that false its. to get the method defined only for the class
ClassTester.methods.count => 108
See, the argument 'false' is important to get the class's methods.
But what about the instance methods out there.
- Instance methods
ClassTester.instance_methods false => [:inst_one, :inst_one=, :inst_two, :inst_two=, :hello_public, :pro_me]
Note: see :inst_one= .. will talk about these later
- Get the public, private and protected methods.
inst = ClassTester.new
inst.instance_of? ClassTester => true
4.1 public_methods, private and protected methods. instance.*_methods
inst.public_methods false => [:inst_one, :inst_one=, :inst_two, :inst_two=, :hello_public] inst.private_methods false => [:private_method]
- upcoming topics
we will talk about *_get *_set *_defined?
Let my enjoy my break, today i will goto the village.