Skip to content
Browse files

adding koans from: Metaprogramming in Ruby: It’s All About the Self (h…

  • Loading branch information...
1 parent 9df3391 commit 5ee2a22aeab802f91c0f6dc520cd617cf4c7d2d7 @sathish316 sathish316 committed Aug 31, 2011
Showing with 253 additions and 3 deletions.
  1. +124 −0 koans/about_self.rb
  2. +1 −1 koans/about_singleton_methods.rb
  3. +2 −1 koans/path_to_enlightenment.rb
  4. +124 −0 src/about_self.rb
  5. +2 −1 src/path_to_enlightenment.rb
View
124 koans/about_self.rb
@@ -0,0 +1,124 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+# Based on Yehuda Katz's article: Metaprogramming in Ruby: It's all about the self
+# http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/
+
+class AboutSelf < EdgeCase::Koan
+
+ class Person
+ def self.species
+ "Homo Sapiens"
+ end
+ end
+
+ def test_self_inside_class_defines_class_method
+ assert_equal __, Person.species
+ end
+
+ @@self_inside_class = class Person
+ self
+ end
+
+ def test_self_inside_class_is_the_class_itself
+ assert_equal __, @@self_inside_class
+ end
+
+ class Dog ; end
+
+ class << Dog
+ def species
+ "Canis Lupus"
+ end
+ end
+
+ def test_self_inside_class_ltlt_class_is_the_metaclass
+ assert_equal __, Dog.species
+ end
+
+ class Cat
+ class << self
+ def species
+ "Felis Catus"
+ end
+ end
+ end
+
+ def test_self_inside_class_ltlt_self_is_the_metaclass
+ # inside class self = Cat
+ # class << self is same as class << Cat
+ assert_equal __, Cat.species
+ end
+
+ class Lion ; end
+
+ @@object = Lion.new
+ @@self_inside_instance_eval_of_object = @@object.instance_eval { self }
+
+ def test_self_inside_instance_eval_of_object_is_the_object_itself
+ assert_equal __, @@self_inside_instance_eval_of_object
+ end
+
+ @@self_inside_instance_eval_of_class = Lion.instance_eval { self }
+
+ def test_self_inside_instance_eval_of_class_is_the_class_itself
+ assert_equal __, @@self_inside_instance_eval_of_class
+ end
+
+ Lion.instance_eval do
+ def species
+ "Panthera Leo"
+ end
+ end
+
+ def test_self_inside_instance_eval_is_class_and_defines_class_methods
+ assert_equal __, Lion.species
+ end
+
+ class Tiger ; end
+
+ def Tiger.species
+ "Panthera Tigris"
+ end
+
+ def test_singleton_method_on_Class_defines_a_class_method
+ assert_equal __, Tiger.species
+ end
+
+ class Person
+ def name
+ "Matz"
+ end
+ end
+
+ def test_methods_defined_in_a_class_are_instance_methods
+ assert_equal __, Person.new.name
+ end
+
+ @@self_inside_class_eval = Cat.class_eval { self }
+
+ def test_self_inside_class_eval_is_the_class_itself
+ assert_equal __, @@self_inside_class_eval
+ end
+
+ Cat.class_eval do
+ def name
+ "Frisky"
+ end
+ end
+
+ def test_class_eval_defines_methods_in_class
+ assert_equal __, Cat.new.name
+ end
+
+ class ::Class
+ def loud_name
+ "#{name.upcase}"
+ end
+ end
+
+ def test_methods_defined_in_Class_class_is_available_to_all_classes
+ # All classes are subclasses of Class and inherit methods of Class
+ assert_match __, Person.loud_name
+ end
+
+end
View
2 koans/about_singleton_methods.rb
@@ -12,7 +12,7 @@ def test_instance_method_calls_method_on_parent_class_if_not_found_in_class
assert_equal __, animal.frozen?
end
- def test_singleton_method_calls_method_on_anonymous_or_ghost_or_eigen_class
+ def test_singleton_method_calls_method_on_anonymous_or_ghost_or_eigen_or_meta_class
animal = "cat"
def animal.speak
"miaow"
View
3 koans/path_to_enlightenment.rb
@@ -3,6 +3,7 @@
$LOAD_PATH << File.dirname(__FILE__)
require 'about_metaclass'
+require 'about_self'
require 'about_singleton_methods'
require 'about_class_as_constant'
require 'about_class_methods'
@@ -14,4 +15,4 @@
require 'about_define_method'
require 'about_instance_eval_and_class_eval'
require 'about_hook_methods'
-require 'about_method_added'
+require 'about_method_added'
View
124 src/about_self.rb
@@ -0,0 +1,124 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+# Based on Yehuda Katz's article: Metaprogramming in Ruby: It's all about the self
+# http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/
+
+class AboutSelf < EdgeCase::Koan
+
+ class Person
+ def self.species
+ "Homo Sapiens"
+ end
+ end
+
+ def test_self_inside_class_defines_class_method
+ assert_equal "Homo Sapiens", Person.species
+ end
+
+ @@self_inside_class = class Person
+ self
+ end
+
+ def test_self_inside_class_is_the_class_itself
+ assert_equal Person, @@self_inside_class
+ end
+
+ class Dog ; end
+
+ class << Dog
+ def species
+ "Canis Lupus"
+ end
+ end
+
+ def test_self_inside_class_ltlt_class_is_the_metaclass
+ assert_equal "Canis Lupus", Dog.species
+ end
+
+ class Cat
+ class << self
+ def species
+ "Felis Catus"
+ end
+ end
+ end
+
+ def test_self_inside_class_ltlt_self_is_the_metaclass
+ # inside class self = Cat
+ # class << self is same as class << Cat
+ assert_equal "Felis Catus", Cat.species
+ end
+
+ class Lion ; end
+
+ @@object = Lion.new
+ @@self_inside_instance_eval_of_object = @@object.instance_eval { self }
+
+ def test_self_inside_instance_eval_of_object_is_the_object_itself
+ assert_equal @@object, @@self_inside_instance_eval_of_object
+ end
+
+ @@self_inside_instance_eval_of_class = Lion.instance_eval { self }
+
+ def test_self_inside_instance_eval_of_class_is_the_class_itself
+ assert_equal Lion, @@self_inside_instance_eval_of_class
+ end
+
+ Lion.instance_eval do
+ def species
+ "Panthera Leo"
+ end
+ end
+
+ def test_self_inside_instance_eval_is_class_and_defines_class_methods
+ assert_equal "Panthera Leo", Lion.species
+ end
+
+ class Tiger ; end
+
+ def Tiger.species
+ "Panthera Tigris"
+ end
+
+ def test_singleton_method_on_Class_defines_a_class_method
+ assert_equal "Panthera Tigris", Tiger.species
+ end
+
+ class Person
+ def name
+ "Matz"
+ end
+ end
+
+ def test_methods_defined_in_a_class_are_instance_methods
+ assert_equal "Matz", Person.new.name
+ end
+
+ @@self_inside_class_eval = Cat.class_eval { self }
+
+ def test_self_inside_class_eval_is_the_class_itself
+ assert_equal Cat, @@self_inside_class_eval
+ end
+
+ Cat.class_eval do
+ def name
+ "Frisky"
+ end
+ end
+
+ def test_class_eval_defines_methods_in_class
+ assert_equal "Frisky", Cat.new.name
+ end
+
+ class ::Class
+ def loud_name
+ "#{name.upcase}"
+ end
+ end
+
+ def test_methods_defined_in_Class_class_is_available_to_all_classes
+ # All classes are subclasses of Class and inherit methods of Class
+ assert_match "PERSON", Person.loud_name
+ end
+
+end
View
3 src/path_to_enlightenment.rb
@@ -3,6 +3,7 @@
$LOAD_PATH << File.dirname(__FILE__)
require 'about_metaclass'
+require 'about_self'
require 'about_singleton_methods'
require 'about_class_as_constant'
require 'about_class_methods'
@@ -14,4 +15,4 @@
require 'about_define_method'
require 'about_instance_eval_and_class_eval'
require 'about_hook_methods'
-require 'about_method_added'
+require 'about_method_added'

0 comments on commit 5ee2a22

Please sign in to comment.
Something went wrong with that request. Please try again.