Monkey patching:
You have so many functions which are inbuilt in Ruby.This language is so flexible and lets you go and edit the functions for a particular program so that it can act as u want it to perform.This is called as monkey patching.This considerably increases the flexibility of Ruby ,which when over engineered may lead to many inconsistencies.
Array manipulations:
You can change the way .each function executes by
class Array
def each
puts "before printing"
yield(self)
puts "After printing"
end
end
a1=[1,2,3]
a1.each{|q| puts q}
Output:
before printing
1
2
3
After printing
This is just a simple manipulation ,you can manipulate it to do anything you prefer to.Similar manipulations can be done to Ranges to using the functions available to ranges.
Scope:
There are different scopes available in Ruby.All methods are public by default.There are other access specifiers like protected,private etc.Both private and public are almost the same.The other details regarding the scope are already mentioned in the relative sections in the previous posts.
Iterators In Ruby:
Iterators are something which is used to traverse through an array again and again repeatedly.For String class we have iterators like "each_byte" to traverse char by char and "each_line" to traverse between the different lines.For different classes different set of methods are provided.In low level lang like c while iterating the string you should check for the termination char to give it as a condition but this doesnt require any internal knowledge of the atorage.
a.each{|q| puts q if q<5}
Attributes:
I have already specified about the accessors that are used for accessing ruby attributes from outside the class.You can either use the predefined one or you can define your own attribute accessors.Both methods are specified in my previous posts.
Exception Handling:
Exception handler is a group of code that is executed when something goes not as expected.When an exception occurs it raises an exception which breaks the normal flow of operations.This exception is rescued by "rescue " and is handled.Any exception raised is an instance of class "Exception"
class exc_test
def div
x=0
raise a=b/x
rescue
puts "im rescued"
ensure
puts "I was not let go"
end
end
The "ensure" is like finally" in java,"raise" is like throw,and "rescue" is like catch.
If all rescues fail "ensure" will catch the exception..
You can also create other exceptions of your own which inherits from Exception class.
class Myownexception<Exception
end
e1=Myownexception.new
raise MyownException
rescue Myownexception=>w
puts "im caught by my rescuer"
end
raise 4/0
rescue
puts "im rescued by zerodiverror"
end
Standard Library functions:
There are many inbuilt functions which are included together in a library which can be required and included and the methods can be used.Whenever you want specific methods check the ruby documentations and use it.
SCalars Date and TIme:
Date can be identified using Time class..The following example gives you a gist.
Another method is used to set and get variables
object.instance_variable_get(geek)
You have so many functions which are inbuilt in Ruby.This language is so flexible and lets you go and edit the functions for a particular program so that it can act as u want it to perform.This is called as monkey patching.This considerably increases the flexibility of Ruby ,which when over engineered may lead to many inconsistencies.
Array manipulations:
You can change the way .each function executes by
class Array
def each
puts "before printing"
yield(self)
puts "After printing"
end
end
a1=[1,2,3]
a1.each{|q| puts q}
Output:
before printing
1
2
3
After printing
This is just a simple manipulation ,you can manipulate it to do anything you prefer to.Similar manipulations can be done to Ranges to using the functions available to ranges.
Scope:
There are different scopes available in Ruby.All methods are public by default.There are other access specifiers like protected,private etc.Both private and public are almost the same.The other details regarding the scope are already mentioned in the relative sections in the previous posts.
Iterators In Ruby:
Iterators are something which is used to traverse through an array again and again repeatedly.For String class we have iterators like "each_byte" to traverse char by char and "each_line" to traverse between the different lines.For different classes different set of methods are provided.In low level lang like c while iterating the string you should check for the termination char to give it as a condition but this doesnt require any internal knowledge of the atorage.
a.each{|q| puts q if q<5}
Attributes:
I have already specified about the accessors that are used for accessing ruby attributes from outside the class.You can either use the predefined one or you can define your own attribute accessors.Both methods are specified in my previous posts.
Exception Handling:
Exception handler is a group of code that is executed when something goes not as expected.When an exception occurs it raises an exception which breaks the normal flow of operations.This exception is rescued by "rescue " and is handled.Any exception raised is an instance of class "Exception"
class exc_test
def div
x=0
raise a=b/x
rescue
puts "im rescued"
ensure
puts "I was not let go"
end
end
The "ensure" is like finally" in java,"raise" is like throw,and "rescue" is like catch.
If all rescues fail "ensure" will catch the exception..
You can also create other exceptions of your own which inherits from Exception class.
class Myownexception<Exception
end
e1=Myownexception.new
raise MyownException
rescue Myownexception=>w
puts "im caught by my rescuer"
end
raise 4/0
rescue
puts "im rescued by zerodiverror"
end
Standard Library functions:
There are many inbuilt functions which are included together in a library which can be required and included and the methods can be used.Whenever you want specific methods check the ruby documentations and use it.
SCalars Date and TIme:
Date can be identified using Time class..The following example gives you a gist.
time = Time.new # Components of a Time puts "Current Time : " + time.inspect puts time.year # => Year of the date puts time.month # => Month of the date (1 to 12) puts time.day # => Day of the date (1 to 31 ) puts time.wday # => 0: Day of week: 0 is Sunday puts time.yday # => 365: Day of year puts time.hour # => 23: 24-hour clock puts time.min # => 59 puts time.sec # => 59 puts time.usec # => 999999: microseconds puts time.zone # => "UTC": timezone name
Instance_eval and class_eval:
Instance_eval has 2 different uses
1)Setting and getting the instance variables.
object_name.instance_eval do
puts @geek
end
2)It is used to define class methods
classname.instance_eval do
def who
//class methods content
end
puts classname.who
Class_eval:
1)setting and getting class variables
puts classname.class_eval("@geek")
classname.class_eval do
@@geek="fds"
end
2)Defining instance methods
classname.class_eval do
//instance methods
end
Another method is used to set and get variables
object.instance_variable_get(geek)
object.instance_variable_set(geek="rona")
No comments:
Post a Comment