Class: Range

Inherits:
Object show all
Defined in:
lib/carat-dev/range.rb

Instance Method Summary collapse

Constructor Details

#initialize(first, halt, exclude_last = false, &succ) ⇒ Range

Returns a new instance of Range.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/carat-dev/range.rb', line 69

def initialize( first, halt, exclude_last=false, &succ )
  @first = first
  @last = halt.kind_of?(Proc) ? nil : halt
  @halt = @last ? nil : halt
  #@exclude_first = exclude_first
  @exclude_last = exclude_last
  @direction = (@last <=> @first)
  #@direction = 1 if @direction == 0
  @succ = succ || nil #@first.method(:succ).to_proc
  #@halt = nil #exclude_last? ? proc{ |x| x.cmp(@last) != -1 } : proc{ |x| x.cmp(@last) == 1 }
  initialize_simple(@first, @last, @exclude_last)
end

Instance Method Details

#-@Object

poor names undef_method :begin undef_method :end undef_method :exclude_end?



131
# File 'lib/carat-dev/range.rb', line 131

def -@ ;  Range.new( @first, @last, true, &@succ ) ; end

#between?(x) ⇒ Boolean Also known as: ===

between? simply tests whether the given value falls between the sentinels by comparison. It does not verify that the value is acutally in the range’s sequence.

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/carat-dev/range.rb', line 151

def between?(x)
  #tf = exclude_first? ? 1 : 0
  tl = exclude_last? ? -1 : 0
  # if other classes handled Infinity in their <=> method
  # (which probably they should) this clause would not be required
  if first.kind_of?(InfinityClass)
    ft = ((first <=> x) <= tf)
  else
    ft = (x <=> first) >= tf
  end
  if last.kind_of?(InfinityClass)
    fl = ((last <=> x) >= tl)
  else
    fl = (x <=> last) <= tl
  end
  ft && fl
end

#directionObject



122
123
124
# File 'lib/carat-dev/range.rb', line 122

def direction
 @direction ||= (@last <=> @first)
end

#distanceObject Also known as: length, size



133
134
135
136
# File 'lib/carat-dev/range.rb', line 133

def distance
  return @last.distance(@first) if @last and @last.respond_to?(:distance)
  self.to_a.length # should it just be nil instead?
end

#each(&blk) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/carat-dev/range.rb', line 191

def each( &blk )
  return each_succ( &blk ) unless @succ or @halt
  if @halt
    halt_proc = @halt
  else
    halt_proc = exclude_last? ? proc{ |x| x.cmp(@last) != -1 } : proc{ |x| x.cmp(@last) == 1 }
  end   
  #t = exclude_last? ? -1 : 0
  s = @first
  if @succ
    #while (s <=> @last) == -1 or (s <=> @last) == t
    until halt_proc.call(s)
      yield(s)
      @succ.call(s)
    end
  else
    #while (s <=> @last) == -1 or (s <=> @last) == t
    until halt_proc.call(s)
      yield(s)
      s.succ
    end      
  end
end

#each_succObject



190
# File 'lib/carat-dev/range.rb', line 190

alias each_succ each

#eql?(other) ⇒ Boolean

Compares two ranges to see if they are equal

Returns:

  • (Boolean)


177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/carat-dev/range.rb', line 177

def eql?(other)
  return false unless @first == other.first
  other_last = other.exclude_last? ? other.last : other.last
  if self.exclude_last? and not other.exclude_last?
    return false unless self.last == other.last.succ
  elsif other.exclude_last? and not self.exclude_last?
    return false unless self.last.succ == other.last
  else
    return false unless @last == other.last
  end
  true
end

#haltObject



97
# File 'lib/carat-dev/range.rb', line 97

def halt ; @halt ; end

#halt=(halt_proc) ⇒ Object

Raises:

  • (ArgumentError)


111
112
113
114
# File 'lib/carat-dev/range.rb', line 111

def halt=(halt_proc)
  raise ArgumentError unless Proc === halt_proc
  @halt = halt_proc
end

#halt?(s) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/carat-dev/range.rb', line 99

def halt?(s)
  if @halt
    @halt.call(s)
  else
   if @exclude_last
     s.cmp(@last) != -1
   else
     s.cmp(@last) == 1
   end
  end
end

#maxObject



144
145
146
# File 'lib/carat-dev/range.rb', line 144

def max
  (@first.cmp(@last) == 1) ? @first : @last
end

#minObject



140
141
142
# File 'lib/carat-dev/range.rb', line 140

def min
  (@first.cmp(@last) == -1) ? @first : @last
end

#succObject



82
# File 'lib/carat-dev/range.rb', line 82

def succ ; @succ ; end

#succ!(s) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/carat-dev/range.rb', line 84

def succ!(s)
 if @succ
   @succ.call(s)
 else
   s.succ
 end
end

#succ=(succ_proc) ⇒ Object

Raises:

  • (ArgumentError)


92
93
94
95
# File 'lib/carat-dev/range.rb', line 92

def succ=(succ_proc)
  raise ArgumentError unless Proc === succ_proc
  @succ = succ_proc
end