Permalink
Browse files

Modifying returning comments and directory structure.

  • Loading branch information...
1 parent d70ad67 commit 2b7169e09540722d70c8933fd7c524b8f56bdc61 @reinh committed Jun 4, 2008
Showing with 43 additions and 29 deletions.
  1. +0 −8 returning/category1.rb
  2. +0 −8 returning/category2.rb
  3. +0 −13 returning/returning.markdown
  4. +43 −0 returning/returning.rb
View
@@ -1,8 +0,0 @@
-class Category
- def display_name
- ret = ""
- ret << number.to_s
- ret << " - #{title}" if title
- ret
- end
-end
View
@@ -1,8 +0,0 @@
-class Category
- def display_name
- returning "" do |ret|
- ret << number.to_s
- ret << " - #{title}" if title
- end
- end
-end
@@ -1,13 +0,0 @@
-Returning
-=========
-
-Returning is a [RubyOnRails][1] idiom that provides a closure around the common accumulator temp variable pattern. Typical use is as such:
-
- returning "" do |accumulator|
- accumulator << "<span>"
- accumulator << "This is some text"
- accumulator << "</span>
- end
-
-
- [1]: http://rubyonrails.org
View
@@ -0,0 +1,43 @@
+class Object
+ # Returning
+ # =========
+ #
+ # Returning is a RubyOnRails idiom that provides a closure around the common
+ # accumulator temp variable pattern.
+ def returning(accumulator)
+ yield accumulator
+ accumulator
+ end
+end
+
+class Category
+ def number; rand(100); end
+end
+
+# AccumulatingCategory
+# Implements #display_name using an accumulator temp variable.
+class AccumulatingCategory < Category
+ def title; "Accumulating Sucks"; end
+
+ def display_name
+ ret = ""
+ ret << number.to_s if number
+ ret << " - #{title}" if title
+ ret
+ end
+end
+AccumulatingCategory.new.display_name # => "91 - Accumulating Sucks"
+
+# ReturningCategory
+# Implements #display_name using the returning pattern.
+class ReturningCategory < Category
+ def title; "Returning Rules"; end
+
+ def display_name
+ returning "" do |ret|
+ ret << number.to_s if number
+ ret << " - #{title}" if title
+ end
+ end
+end
+ReturningCategory.new.display_name # => "50 - Returning Rules"

0 comments on commit 2b7169e

Please sign in to comment.