Module: Enumerable
- Defined in:
- lib/builtinME.rb
Instance Method Summary collapse
- #collect ⇒ Object (also: #map)
- #each_with_index ⇒ Object
- #find_all ⇒ Object (also: #select)
- #inject(*args) ⇒ Object
- #max(&proc) ⇒ Object
- #member?(other) ⇒ Boolean (also: #include?)
- #min(&proc) ⇒ Object
- #sort(&proc) ⇒ Object
- #sort_by(&block) ⇒ Object
- #to_a ⇒ Object (also: #entries)
Instance Method Details
#collect ⇒ Object Also known as: map
140 141 142 143 144 |
# File 'lib/builtinME.rb', line 140 def collect arr = [] each{|obj| arr << yield(obj)} return arr end |
#each_with_index ⇒ Object
93 94 95 96 |
# File 'lib/builtinME.rb', line 93 def each_with_index i = 0; each {|x| yield x, i; i = i + 1} end |
#find_all ⇒ Object Also known as: select
74 75 76 77 78 79 80 81 82 |
# File 'lib/builtinME.rb', line 74 def find_all a = [] each {|x| if yield x a.push(x) end } return a end |
#inject(*args) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/builtinME.rb', line 125 def inject(*args) if args.size == 0 then vals = to_a memo = vals[0] vals[1..vals.size-1].each {|obj| memo = yield(memo, obj)} return memo elsif args.size == 1 then memo = args[0] each {|obj| memo = yield(memo, obj)} return memo else nil end end |
#max(&proc) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/builtinME.rb', line 148 def max(&proc) proc = lambda { |a, b| a <=> b } unless block_given? max = nil each {|obj| max = obj if max.nil? || proc.call(max, obj) < 0} max end |
#member?(other) ⇒ Boolean Also known as: include?
118 119 120 121 |
# File 'lib/builtinME.rb', line 118 def member?(other) each{|obj| return true if obj == other } return false end |
#min(&proc) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/builtinME.rb', line 155 def min(&proc) proc = lambda { |a, b| a <=> b } unless block_given? min = nil each {|obj| min = obj if min.nil? || proc.call(min, obj) > 0} min end |
#sort(&proc) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/builtinME.rb', line 106 def sort(&proc) #proc = lambda{ |a,b| a<=>b } unless block_given? arr = to_a arr.sort{|x,y| if block_given? then proc.call(x,y) else x<=>y end } end |
#sort_by(&block) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/builtinME.rb', line 86 def sort_by &block array_of_tuples = [] each {|x| array_of_tuples.push([x, yield(x)])} array_of_tuples = array_of_tuples.sort {|x, y| x[1] <=> y[1]} return array_of_tuples.collect {|x| x[0]} end |
#to_a ⇒ Object Also known as: entries
98 99 100 101 102 |
# File 'lib/builtinME.rb', line 98 def to_a arr = [] each{|obj| arr <<obj} return arr end |