devdaily home | apple | java | perl | unix | directory | blog

What this is

This file is included in the DevDaily.com "Ruby Source Code Warehouse" project. The intent of this project is to help you "Learn Ruby by Example" TM.

Other links

The source code

require 'access+'

class Test

  feature :None # private methods

    attr_accessor :attribute

    def privatemethod
      puts "you're in a private method!"
    end

  allow :Any # public methods

    def publicmethod
      self.attribute ||= 0
      self.attribute += 1
      puts "you're in a public method!"
      puts "attribute is #{self.attribute}"
    end

    def callprivate
      privatemethod
    end

  feature :Test, :Test3 # protected methods

    def protectedmethod
      puts "you're in a protected method!"
    end

  feature :Test2 # friend methods

    def friendmethod
      puts "you're in a friend method!"
    end
end

class SubTest < Test
  def subtestmethod
    privatemethod
  end

  def subtestmethod2
    protectedmethod
  end

  def subtestmethod3
    friendmethod
  end
end

class Test2
  def callfriend( t )
    t.friendmethod
  end
end

class Test3
  def callfriend( t )
    t.friendmethod
  end

  def callprotected( t )
    t.protectedmethod
  end
end

def test( method, *args )
  puts "----------------"
  begin
    method.call *args
  rescue
    puts $!
  end
end

t = Test.new
t2 = Test2.new
t3 = Test3.new
s = SubTest.new

test t.method( :publicmethod )
test t.method( :privatemethod )
test t.method( :callprivate )
test t.method( :attribute )
test s.method( :subtestmethod )
test s.method( :subtestmethod2 )
test s.method( :subtestmethod3 )
test t.method( :publicmethod )
test t.method( :protectedmethod )
test t2.method( :callfriend ), t
test t3.method( :callfriend ), t
test t3.method( :callprotected ), t




Copyright 1998-2008 Alvin Alexander
All Rights Reserved.
 
devdaily.com is based in louisville, kentucky, and this web site is hosted by godaddy.com