Method: Immutable::Headable#fetch
- Defined in:
 - lib/immutable/headable.rb
 
#fetch(n) ⇒ Object #fetch(n, ifnone) ⇒ Object #fetch(n) {|n| ... } ⇒ Object
Returns the nth element of self. If n is out of range, IndexError returned.
      255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281  | 
    
      # File 'lib/immutable/headable.rb', line 255 def fetch(*args) alen = args.length n, ifnone = *args unless (1..2).cover?(alen) raise ArgumentError, "wrong number of arguments (#{alen} for 1..2)" end raise TypeError unless n.respond_to?(:to_int) int = n.to_int return at(int) if int >= 0 && int < length if block_given? if alen == 2 warn "#{__LINE__}:warning: block supersedes default value argument" end yield n else if alen == 2 ifnone else raise IndexError, "index #{int} outside of list bounds" end end end  |