Method: Array#first
- Defined in:
- lib/source/ruby.rb
#first(n) ⇒ Object
call-seq:
ary.first -> object or nil
ary.first(n) -> array
Returns the first element of ary, or an array of the first n elements of ary. If ary is empty, the first form returns nil and the second form returns an empty array.
a = %w(a b c d)
a.first #=> "a"
a.first(1) #=> ["a"]
a.first(3) #=> ["a", "b", "c"]
2260 2261 2262 2263 |
# File 'lib/source/ruby.rb', line 2260 def first(n) `if(n!=null){for(var i=0,l=this.length,result=[],max=l<n?l:n;i<max;++i){result.push(this[i]);};return result;}` return `this.length==0?nil:this[0]` end |