Skip to content
Permalink
Browse files

Merge pull request #507 from kostya/first

Enumerable#first
  • Loading branch information
alex committed Mar 19, 2013
2 parents ba9afd9 + 97c6da6 commit 196b0527212c6460a1820e5b8e89900f259b9e9d
@@ -137,8 +137,12 @@ def delete(obj, &block)
return nil
end

def first
return self[0]
def first(*args)
if args.empty?
self[0]
else
take(*args)
end
end

def flatten(level = -1)
@@ -1,4 +1,14 @@
module Enumerable

def first(*args)
if args.empty?
self.each { |e| return e }
nil
else
take(*args)
end
end

def map
result = []
self.each do |x|
@@ -95,6 +105,7 @@ def detect(ifnone = nil, &block)
alias find detect

def take(n)
n = Topaz.coerce_int(n)
raise ArgumentError.new("attempt to take negative size") if n < 0
result = []
unless n == 0
@@ -21,6 +21,14 @@ def each(&block)
self
end

def first(*args)
if args.empty?
self.begin
else
take(*args)
end
end

def ===(value)
self.include?(value)
end
@@ -1,11 +0,0 @@
fails:Array#first returns the first count elements if given a count
fails:Array#first returns an empty array when passed count on an empty array
fails:Array#first returns an empty array when passed count == 0
fails:Array#first returns an array containing the first element when passed count == 1
fails:Array#first returns the entire array when count > length
fails:Array#first returns an array which is independent to the original when passed count
fails:Array#first tries to convert the passed argument to an Integer using #to_int
fails:Array#first raises a TypeError if the passed argument is not numeric
fails:Array#first does not return subclass instance when passed count on Array subclasses
fails:Array#first is not destructive
fails:Array#first

This file was deleted.

@@ -1,12 +1 @@
fails:Enumerable#first returns the first element
fails:Enumerable#first returns nil if self is empty
fails:Enumerable#first when passed an argument returns the first count elements if given a count
fails:Enumerable#first when passed an argument returns an empty array when passed count on an empty array
fails:Enumerable#first when passed an argument returns an empty array when passed count == 0
fails:Enumerable#first when passed an argument returns an array containing the first element when passed count == 1
fails:Enumerable#first when passed an argument raises an ArgumentError when count is negative
fails:Enumerable#first when passed an argument returns the entire array when count > length
fails:Enumerable#first when passed an argument tries to convert the passed argument to an Integer using #to_int
fails:Enumerable#first when passed an argument raises a TypeError if the passed argument is not numeric
fails:Enumerable#first when passed an argument gathers whole arrays as elements when each yields multiple
fails:Enumerable#first when passed an argument consumes only what is needed
@@ -1,3 +1 @@
fails:Enumerable#take when passed an argument tries to convert the passed argument to an Integer using #to_int
fails:Enumerable#take when passed an argument raises a TypeError if the passed argument is not numeric
fails:Enumerable#take when passed an argument gathers whole arrays as elements when each yields multiple

This file was deleted.

@@ -15,3 +15,7 @@ def method_intmask(self, space, w_int):
elif space.is_kind_of(w_int, space.w_bignum):
bigint = space.bigint_w(w_int)
return space.newint(intmask(bigint.uintmask()))

@moduledef.function("coerce_int")
def method_coerce_int(self, space, w_obj):
return space.convert_type(w_obj, space.w_fixnum, "to_int")
@@ -28,7 +28,6 @@ def method_initialize(self, space, w_start, w_end, excl=False):
self.w_end = w_end
self.exclusive = excl

@classdef.method("first")
@classdef.method("begin")
def method_begin(self, space):
return self.w_start

0 comments on commit 196b052

Please sign in to comment.
You can’t perform that action at this time.