public
Description: Ruby 2.0 functionality implemented in Ruby 1.8.
Homepage: http://devblog.michaelgalero.com
Clone URL: git://github.com/mikong/point2.git
Search Repo:
Implemented Object methods public_method, public_send and tap. Added basic 
README.
mikong (author)
Sun Apr 06 03:01:02 -0700 2008
commit  813f6e8b13fb9fd9532295a2bbe9101a8da943f8
tree    b5a0faaeb792eca46efa75e20b45a40a6bb4b9d8
parent  c7a1c0d50f4e868b3fe7919dea19b78a4ffd8480
0
...
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
0
@@ -0,0 +1,7 @@
0
+== Point 2
0
+
0
+Ruby 2.0 functionality implemented in Ruby 1.8.
0
+
0
+The objective of this project is not to port the next version of Ruby to Ruby 1.8. This project was created to play around with metaprogramming. Read this {blog entry}[http://devblog.michaelgalero.com/2008/04/03/ruby-19-hash-in-ruby-18/] on how this project came about.
0
+
0
+== Installation
...
5
6
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
9
10
...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
0
@@ -5,6 +5,36 @@ class Object
0
     send method if respond_to? method
0
   end
0
   
0
+ # Defines a singleton_method in the receiver. The method parameter can be a Proc or
0
+ # Method object. If a block is specified, it is used as the method body. This block
0
+ # is evaluated using instance_eval.
0
+ def define_singleton_method(symbol, method)
0
+ # TODO
0
+ end
0
+
0
+ # Looks up the named public method, returning a Method object (or raising NameError
0
+ # if the method is not found, or if it is found but not public).
0
+ def public_method(symbol)
0
+ unless self.public_methods.include?(symbol.to_s)
0
+ raise NameError.new("undefined public method '#{symbol}' for class '#{self.class}'")
0
+ end
0
+ self.method(symbol)
0
+ end
0
+
0
+ # Same as send but for public methods only.
0
+ def public_send(name, *args)
0
+ unless self.public_methods.include?(name.to_s)
0
+ raise NoMethodError.new("undefined public method '#{name}' for #{self.to_s}", name, *args)
0
+ end
0
+ self.__send__(name, *args)
0
+ end
0
+
0
+ # Invokes the block, passing obj as a parameter. Returns obj. Allows you to write code
0
+ # that takes part in a method chain but that does not affect the overall value of the chain.
0
+ def tap
0
+ yield self
0
+ self
0
+ end
0
 end
0
 
0
 class Array
...
28
29
30
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
33
34
...
28
29
30
 
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
0
@@ -28,7 +28,41 @@ describe "Point2 Project" do
0
       @book = Book.new('Harry Potter')
0
       @book.try(:title).should eql('Harry Potter')
0
     end
0
-
0
+
0
+ it "can be tapped" do
0
+ @reversed_and_capitalized = "dog".reverse.tap {|o| @reversed = o}.capitalize
0
+ @reversed.should == 'god'
0
+ @reversed_and_capitalized.should == 'God'
0
+ end
0
+
0
+ it "can return a public method" do
0
+ @object = Object.new
0
+
0
+ # calling a non-existent method
0
+ lambda do
0
+ @object.public_method(:tatata)
0
+ end.should raise_error(NameError)
0
+
0
+ # calling a private method
0
+ lambda do
0
+ @object.public_method(:format)
0
+ end.should raise_error(NameError)
0
+
0
+ # calling a public method
0
+ @method = @object.public_method(:class)
0
+ @method.call.should == @object.class
0
+ end
0
+
0
+ it "can __send__ *args to a public method" do
0
+ @object = Object.new
0
+
0
+ # send to a private method
0
+ lambda do
0
+ @object.public_send(:format)
0
+ end.should raise_error(NoMethodError)
0
+
0
+ @object.public_send(:class).should == @object.class
0
+ end
0
   end
0
   
0
   describe Array do

Comments

    No one has commented yet.