Module: Enumerable
- Defined in:
- lib/enumerable_extensions.rb
Overview
Enumerable Extensions
- Author
-
Joel Parker Henderson, [email protected]
- Copyright
-
Copyright © 2006-2008 Joel Parker Henderson
- License
-
CreativeCommons License, Non-commercial Share Alike
- License
-
LGPL, GNU Lesser General Public License
Ruby Enumerable base class extensions.
Instance Method Summary collapse
-
#cat(prefix = nil, suffix = nil) ⇒ Object
Concatenate the items into a string.
-
#map_id ⇒ Object
map item => item.id.
-
#map_to_sym ⇒ Object
map item => item.to_sym.
-
#nitems_until ⇒ Object
enum.nitems_until {| obj | block } => number of items Returns the number of leading elements for which block is false.
-
#nitems_while ⇒ Object
enum.nitems_while {| obj | block } => number of items Returns the number of leading elements for which block is not false or nil.
-
#nitems_with_index ⇒ Object
enum.nitems_with_index {|obj,i| block } => number of items Calls block with two arguments, the item and its index, for each item in enum.
-
#select_until ⇒ Object
enum.select_until {|obj| block } => array Returns an array containing the leading elements for which block is false or nil.
-
#select_while ⇒ Object
enum.select_while {|obj| block } => array Returns an array containing the leading elements for which block is not false or nil.
-
#select_with_index ⇒ Object
enum.select_with_index {|obj,i| block } => array Calls block with two arguments, the item and its index, for each item in enum.
Instance Method Details
#cat(prefix = nil, suffix = nil) ⇒ Object
Concatenate the items into a string.
Example
arr=['anne','beth','cate']
arr.cat => "annebethcate"
You can optionally provide a prefix and suffix.
Example
arr.cat("+") => "+anne+beth+cate"
arr.cat("+","-") => "+anne-+beth-+cate-"
You can easily wrap items in HTML tags.
Example
arr=['anne','beth','cate']
arr.cat("<li>","</li>\n")
=>
<li>anne</li>
<li>beth</li>
<li>cate</li>
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/enumerable_extensions.rb', line 140 def cat(prefix=nil,suffix=nil) if prefix if suffix inject(""){|sum,s| sum += prefix + s.to_s + suffix} else inject(""){|sum,s| sum += prefix + s.to_s } end else inject(""){|sum,s| sum += s.to_s } end end |
#map_id ⇒ Object
map item => item.id
22 23 24 |
# File 'lib/enumerable_extensions.rb', line 22 def map_id map{|x| x.id} end |
#map_to_sym ⇒ Object
map item => item.to_sym
28 29 30 |
# File 'lib/enumerable_extensions.rb', line 28 def map_to_sym map{|x| x.to_sym} end |
#nitems_until ⇒ Object
enum.nitems_until {| obj | block } => number of items Returns the number of leading elements for which block is false.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/enumerable_extensions.rb', line 94 def nitems_until n = 0 each{|x| if yield(x) break else n+=1 end } return n irb end |
#nitems_while ⇒ Object
enum.nitems_while {| obj | block } => number of items Returns the number of leading elements for which block is not false or nil.
85 86 87 88 89 |
# File 'lib/enumerable_extensions.rb', line 85 def nitems_while n = 0 each{|x| yield(x) ? (n+=1) : break} return n end |
#nitems_with_index ⇒ Object
enum.nitems_with_index {|obj,i| block } => number of items Calls block with two arguments, the item and its index, for each item in enum. Returns the number of leading elements for which block is true.
111 112 113 114 115 |
# File 'lib/enumerable_extensions.rb', line 111 def nitems_with_index i = 0 each{|x| yield(x,i) ? (i+=1) : break} return i end |
#select_until ⇒ Object
enum.select_until {|obj| block } => array Returns an array containing the leading elements for which block is false or nil.
51 52 53 54 55 |
# File 'lib/enumerable_extensions.rb', line 51 def select_until a = [] each{|x| yield(x) ? break : (a << x)} return a end |
#select_while ⇒ Object
enum.select_while {|obj| block } => array Returns an array containing the leading elements for which block is not false or nil.
42 43 44 45 46 |
# File 'lib/enumerable_extensions.rb', line 42 def select_while a = [] each{|x| yield(x) ? (a << x) : break} return a end |
#select_with_index ⇒ Object
enum.select_with_index {|obj,i| block } => array Calls block with two arguments, the item and its index, for each item in enum. Returns an array containing the leading elements for which block is not false or nil.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/enumerable_extensions.rb', line 61 def select_with_index i = 0 a = [] each{|x| if yield(x,i) a << x i+=1 else break end } return a end |