Array value to get except first two values

15 views
Skip to first unread message

Maddy

unread,
Sep 15, 2012, 6:21:50 AM9/15/12
to rubyonra...@googlegroups.com
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....

Carlos Mathiasen

unread,
Sep 15, 2012, 6:38:57 AM9/15/12
to rubyonra...@googlegroups.com

[1,2,3,4].select{|n| ![1,2].include? n}

:-)

---
Send from my cellphone

Иван Бишевац

unread,
Sep 15, 2012, 7:40:35 AM9/15/12
to rubyonra...@googlegroups.com
[1,2,3,4,5,6,7,8 ][0..1]

Generally good advice is to bookmark this page.

2012/9/15 Maddy <ashok...@shriramits.com>
[1,2,3,4,5,6,7,8 ]

Hassan Schroeder

unread,
Sep 15, 2012, 10:33:06 AM9/15/12
to rubyonra...@googlegroups.com
Advisory #1: READ THE DOCS FOR ARRAY.

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

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Jordon Bedwell

unread,
Sep 15, 2012, 10:40:07 AM9/15/12
to rubyonra...@googlegroups.com
On Sat, Sep 15, 2012 at 9:33 AM, Hassan Schroeder
<hassan.s...@gmail.com> wrote:
> On Sat, Sep 15, 2012 at 3:21 AM, Maddy <ashok...@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

Could you not just do [1, 2, 3, 4, 5, 6, 7, 8][2..-1]

Yong Gu

unread,
Sep 15, 2012, 6:25:17 AM9/15/12
to rubyonra...@googlegroups.com
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()

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/10r_aye1o4IJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rob Biedenharn

unread,
Sep 15, 2012, 2:19:03 PM9/15/12
to rubyonra...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages