Forum: Ruby on Rails Array value to get except first two values

Posted by Maddy (Guest)
on 2012-09-15 12:22
(Received via mailing list)
Hi folks,

In an array value i need to find except first two values,

example:
[1,2,3,4,5,6,7,8 ]

in that i need to get values except [1,2].

How can i get it from this array??

please advise....
Posted by Carlos Mathiasen (Guest)
on 2012-09-15 12:39
(Received via mailing list)
[1,2,3,4].select{|n| ![1,2].include? n}

:-)

 ---
Send from my cellphone
Posted by "Иван Бишевац" <ivan.bisevac@gmail.com> (Guest)
on 2012-09-15 13:42
(Received via mailing list)
[1,2,3,4,5,6,7,8 ][0..1]

Look at documentation
http://ruby-doc.org/core-1.9.3/Array.html#method-i-5B-5D.
Generally good advice is to bookmark this 
<http://ruby-doc.org/core-1.9.3/>
 page.

2012/9/15 Maddy <ashokkumar@shriramits.com>
Posted by Hassan Schroeder (Guest)
on 2012-09-15 16:34
(Received via mailing list)
On Sat, Sep 15, 2012 at 3:21 AM, Maddy <ashokkumar@shriramits.com> 
wrote:

> In an array value i need to find except first two values,
>
> example:
> [1,2,3,4,5,6,7,8 ]
>
> in that i need to get values except [1,2].
>
> How can i get it from this array??
>
> please advise....

Advisory #1: READ THE DOCS FOR ARRAY.

Advisory #2:  [1,2,3,4,5,6,7,8].drop 2

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Posted by Jordon Bedwell (Guest)
on 2012-09-15 16:41
(Received via mailing list)
On Sat, Sep 15, 2012 at 9:33 AM, Hassan Schroeder
<hassan.schroeder@gmail.com> wrote:
>>
>> please advise....
>
> Advisory #1: READ THE DOCS FOR ARRAY.
>
> Advisory #2:  [1,2,3,4,5,6,7,8].drop 2

Could you not just do [1, 2, 3, 4, 5, 6, 7, 8][2..-1]
Posted by Yong Gu (Guest)
on 2012-09-15 18:40
(Received via mailing list)
If you are using Rails,

%w( a b c d ).from(0)  # => %w( a b c d )
%w( a b c d ).from(2)  # => %w( c d )
%w( a b c d ).from(10) # => %w()
%w().from(0)           # => %w()
Posted by Rob Biedenharn (Guest)
on 2012-09-15 20:20
(Received via mailing list)
And, of course, you can just you plain 'ol Ruby for this too:

a = [1,2,3,4,5]
a[2..-1] # => [3,4,5]

%w[ a b c d e ][3..-1] #=> ['d', 'e']

-Rob
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.