Yeah so coming to the main language,now i will be introducing and explaining some few basic concepts of ruby.Websites running on RubyonRails are Twitter,Hulu,Yellowpages etc.
What makes it special???
-->It is flexible and easy to learn.
-->Dynamic typing,Object oriented language.
-->NO specific Datatypes
-->Everything is an object in Ruby.
There are various inbuilt functions available in Ruby.The "Class" itself is an object of the type "class".Once you create a variable you can know the class to which it belongs to with the help of
VARIABLE.class.And you can access the methods applicable to it with the help of VARIABLE.methods.The list of methods is vast and cannot be specified here .It can be checked at
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-each_index
Object oriented programming is well established in Ruby.In the class there are different variables involved.
-->Global variables:
This has global scope for the entire program.Starts with a $ sign.
-->Class variable:Will be inside a class and outside a method.Starts with "@@".It is accessed through a class not an instance.
-->Instance Variables:This is the general attributes of a class.Starts with "@".
-->Local Variables:It is always local to a particular block.
In Ruby function definitions and class definitions end with "end" keyword.All the variables declared within the class are private to the class and cannot be accessed outside.In order to access outside
-->Either a method should be created setting and retrieving the attribute values.
-->Use the accessor methods.
ie
if weight is a instance variable give
attr_accessor :height..
Now outside the class you can access the variable"height"
give
"object1.height " and there you get the answer..
Instead of attr_accessor use a combinatiuon of attr_reader and attr_writer.
When we use methods to access attributes you can make it more user understandable this way.Suppose you are getting and setting a gender of an object.
class aa
def gender=(value)
@gen=value
end
def gender
puts @gen..
OUtside the class,give
obj1.gender=50
gen_user=obj1.gender.
There are other special methods with which you can send data from an object to a class..
What makes it special???
-->It is flexible and easy to learn.
-->Dynamic typing,Object oriented language.
-->NO specific Datatypes
-->Everything is an object in Ruby.
There are various inbuilt functions available in Ruby.The "Class" itself is an object of the type "class".Once you create a variable you can know the class to which it belongs to with the help of
VARIABLE.class.And you can access the methods applicable to it with the help of VARIABLE.methods.The list of methods is vast and cannot be specified here .It can be checked at
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-each_index
Object oriented programming is well established in Ruby.In the class there are different variables involved.
-->Global variables:
This has global scope for the entire program.Starts with a $ sign.
-->Class variable:Will be inside a class and outside a method.Starts with "@@".It is accessed through a class not an instance.
-->Instance Variables:This is the general attributes of a class.Starts with "@".
-->Local Variables:It is always local to a particular block.
In Ruby function definitions and class definitions end with "end" keyword.All the variables declared within the class are private to the class and cannot be accessed outside.In order to access outside
-->Either a method should be created setting and retrieving the attribute values.
-->Use the accessor methods.
ie
if weight is a instance variable give
attr_accessor :height..
Now outside the class you can access the variable"height"
give
"object1.height " and there you get the answer..
Instead of attr_accessor use a combinatiuon of attr_reader and attr_writer.
When we use methods to access attributes you can make it more user understandable this way.Suppose you are getting and setting a gender of an object.
class aa
def gender=(value)
@gen=value
end
def gender
puts @gen..
OUtside the class,give
obj1.gender=50
gen_user=obj1.gender.
There are other special methods with which you can send data from an object to a class..
No comments:
Post a Comment