December 18, 2012

Meta-programming in Ruby : Part 1 : Alias


There are altogether 16 posts on this topic of meta-programming in Ruby language. Our approach encourages hands-on experimentation. The exercises here are specially designed to facilitate self-study and other independent modes of learning.

Provided we can find time amidst our busy schedule(well, who is not busy these days?), we will also discuss meta-programming in other languages such as Lisp and python.

Code 1
class A
 def fan;
     puts "fan";
 end
def feast;
     puts "feast";
 end
end
a= A.new
a.fan
a.feast
class A
 alias :flank :fan
 alias :foo :feast
 private :fan
end
begin
a.fan
rescue
 puts ">>>>>>   private method `fan' called for #",
      ">>>>>>   Accessible unless declared \"private\" as above"
end
a.feast
a.foo
a.flank
puts "\n\n"

class Z
 attr_accessor :gear
 alias :get_gear    :gear
 alias :set_gear    :gear=
end

zoe =Z.new
zoe.gear = 34
puts zoe.gear

zoe.set_gear(34)
puts zoe.get_gear 


Results 1
[root@localhost ruby_tutor]# ruby 1alias.rb
fan
feast
>>>>>>   private method `fan' called for #
>>>>>>   Accessible unless declared "private" as above
feast
feast
fan


34
34


Code 2
class String
   alias_method :original_reverse, :reverse

   def reverse
     "Trying to reverse, please wait ... "+ original_reverse
   end
end
p "happy holy days".original_reverse

p "happy holy days".reverse


Results 2
[root@localhost ruby_tutor]# ruby   2alias_method1.rb
"syad yloh yppah"
"Trying to reverse, please wait ... syad yloh yppah"


3 comments:

  1. You cannot just provide a few hundred words and anticipate
    to make cash. Prioritize Blogging around School Responsibilities
    Blog writing and related activities should not be done before homework assignments.
    If you can defend your side satisfactorily, you can anticipate a larger amount of visitors to look at your blogs.


    Also visit my web page :: hostgator coupon

    ReplyDelete
  2. All these symptoms, as well as others, may indicate a brain injury.
    Startup costs for a restaurant will include permits and licenses inevitably before
    more other costs are determined. Debts and assets might talk about what the couple has,
    including all securities, shares, property, cars, and current debts.



    My webpage - Family Lawyers

    ReplyDelete
  3. There are several service providers that are not genuine. Mobile Broadband Connect 5 GB
    give users the ability to access 5 gigabytes of data through a phone tether or phone-based hotspot.
    Another efficient mode to endorse your video is buying buy youtube comments.



    Feel free to surf to my page :: youtube views adder

    ReplyDelete

A Tip for Job Search: Gold Rush Skills

  If you need to make some money very quickly, what would you do? Your answer points to the kind of problems you can solve. They give you so...