Module: Trith::Core::Sequence::Accessors
- Defined in:
- lib/trith/core/sequence.rb
Overview
Sequence accessors.
Instance Method Summary (collapse)
- - (Object) eighth(seq)
- - (Object) fifth(seq)
- - (Object) first(seq)
- - (Object) fourth(seq)
- - (Object) last(seq)
- - (Integer) length(seq)
- - (Object) ninth(seq)
- - (Object) nth(seq, n)
- - (Enumerable) rest(seq)
- - (Object) second(seq)
- - (Object) seventh(seq)
- - (Object) sixth(seq)
- - (Object) tenth(seq)
- - (Object) third(seq)
Instance Method Details
- (Object) eighth(seq)
266 267 268 269 270 271 |
# File 'lib/trith/core/sequence.rb', line 266 def eighth(seq) case when seq.respond_to?(:eighth) then seq.eighth else nth(seq, 7) end end |
- (Object) fifth(seq)
236 237 238 239 240 241 |
# File 'lib/trith/core/sequence.rb', line 236 def fifth(seq) case when seq.respond_to?(:fifth) then seq.fifth else nth(seq, 4) end end |
- (Object) first(seq)
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/trith/core/sequence.rb', line 192 def first(seq) case seq when String seq[0].chr rescue nil # Ruby 1.8 compatible else case when seq.respond_to?(:first) then seq.first else nth(seq, 0) end end end |
- (Object) fourth(seq)
226 227 228 229 230 231 |
# File 'lib/trith/core/sequence.rb', line 226 def fourth(seq) case when seq.respond_to?(:fourth) then seq.fourth else nth(seq, 3) end end |
- (Object) last(seq)
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/trith/core/sequence.rb', line 317 def last(seq) case seq when String seq[-1].chr rescue nil # Ruby 1.8 compatible else case when seq.respond_to?(:last) seq.last when seq.respond_to?(:[]) seq[-1] when seq.respond_to?(:each) seq.each.to_a.last # TODO: optimize else raise Machine::InvalidOperandError.new(seq, :last) end end end |
- (Integer) length(seq)
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/trith/core/sequence.rb', line 159 def length(seq) case when seq.respond_to?(:length) seq.length when seq.respond_to?(:size) seq.size when seq.respond_to?(:each) seq.each.to_a.size # TODO: optimize else raise Machine::InvalidOperandError.new(seq, :length) end end |
- (Object) ninth(seq)
276 277 278 279 280 281 |
# File 'lib/trith/core/sequence.rb', line 276 def ninth(seq) case when seq.respond_to?(:ninth) then seq.ninth else nth(seq, 8) end end |
- (Object) nth(seq, n)
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/trith/core/sequence.rb', line 297 def nth(seq, n) case seq when String seq[n.to_i].chr rescue nil # Ruby 1.8 compatible else case when seq.respond_to?(:nth) seq.nth(n) when seq.respond_to?(:[]) seq[n.to_i] when seq.respond_to?(:each) seq.each.to_a[n.to_i] # TODO: optimize else raise Machine::InvalidOperandError.new(seq, :nth) end end end |
- (Enumerable) rest(seq)
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/trith/core/sequence.rb', line 175 def rest(seq) # TODO: check semantics for empty sequences case when seq.respond_to?(:rest) seq.rest when seq.respond_to?(:[]) seq[1..-1] when seq.respond_to?(:each) seq.each.to_a[1..-1] # TODO: optimize else raise Machine::InvalidOperandError.new(seq, :rest) end end |
- (Object) second(seq)
206 207 208 209 210 211 |
# File 'lib/trith/core/sequence.rb', line 206 def second(seq) case when seq.respond_to?(:second) then seq.second else nth(seq, 1) end end |
- (Object) seventh(seq)
256 257 258 259 260 261 |
# File 'lib/trith/core/sequence.rb', line 256 def seventh(seq) case when seq.respond_to?(:seventh) then seq.seventh else nth(seq, 6) end end |
- (Object) sixth(seq)
246 247 248 249 250 251 |
# File 'lib/trith/core/sequence.rb', line 246 def sixth(seq) case when seq.respond_to?(:sixth) then seq.sixth else nth(seq, 5) end end |
- (Object) tenth(seq)
286 287 288 289 290 291 |
# File 'lib/trith/core/sequence.rb', line 286 def tenth(seq) case when seq.respond_to?(:tenth) then seq.tenth else nth(seq, 9) end end |
- (Object) third(seq)
216 217 218 219 220 221 |
# File 'lib/trith/core/sequence.rb', line 216 def third(seq) case when seq.respond_to?(:third) then seq.third else nth(seq, 2) end end |