[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Real Quick Question...

Jeff Miller

4/2/2008 8:14:00 PM

Hello,
Let's say I have an array like this:

@name_array = ["john", "t.", "smith"]

how can I get the first and LAST entries? IE:, I don't want the middle
one or any in between. I know I can get the first entry by
@name_array[0], but this will be part of a function in which some arrays
will have 2 entries, others 3 or even more, and I just want the last
entry. How can I do this?

Thanks!!
- Jeff Miller
--
Posted via http://www.ruby-....

10 Answers

Jeff Miller

4/2/2008 8:16:00 PM

0

wait... sorry, my brain is tiered... array.first, array.last
--
Posted via http://www.ruby-....

Joel VanderWerf

4/2/2008 8:18:00 PM

0

Jeff Miller wrote:
> Hello,
> Let's say I have an array like this:
>
> @name_array = ["john", "t.", "smith"]
>
> how can I get the first and LAST entries?

[@name_array.first, @name_array.last]

or

@name_array.values_at(0,-1)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Jano Svitok

4/2/2008 8:20:00 PM

0

On Wed, Apr 2, 2008 at 10:13 PM, Jeff Miller <loadeddesigns@gmail.com> wrote:
> Hello,
> Let's say I have an array like this:
>
> @name_array = ["john", "t.", "smith"]
>
> how can I get the first and LAST entries? IE:, I don't want the middle
> one or any in between. I know I can get the first entry by
> @name_array[0], but this will be part of a function in which some arrays
> will have 2 entries, others 3 or even more, and I just want the last
> entry. How can I do this?

quite surprisingly: @name_array.first and @name_array.last :)

Thomas Adam

4/2/2008 8:21:00 PM

0

On 02/04/2008, Jeff Miller <loadeddesigns@gmail.com> wrote:
> Hello,
> Let's say I have an array like this:
>
> @name_array = ["john", "t.", "smith"]
>
> how can I get the first and LAST entries? IE:, I don't want the middle

>> [1,2,3].first
=> 1
>> [1,2,3].last
=> 3
>>

Or have I missed something?

-- Thomas Adam

Jeff Miller

4/2/2008 8:37:00 PM

0

thanks guys, I knew it, my brain just didn't want to cooperate with me
for a minute...

While I'm writing a reply, I have another question: Is there something I
can do to make my script wait for a second or two? Like a pause or
something?

Thanks!
- Jeff
--
Posted via http://www.ruby-....

Abdelkader Boudih

4/2/2008 8:43:00 PM

0

sleep(1) # for 1 second


Jeff Miller wrote:
> thanks guys, I knew it, my brain just didn't want to cooperate with me
> for a minute...
>
> While I'm writing a reply, I have another question: Is there something I
> can do to make my script wait for a second or two? Like a pause or
> something?
>
> Thanks!
> - Jeff
>


Thomas Adam

4/2/2008 8:44:00 PM

0

On 02/04/2008, Jeff Miller <loadeddesigns@gmail.com> wrote:
> thanks guys, I knew it, my brain just didn't want to cooperate with me
> for a minute...
>
> While I'm writing a reply, I have another question: Is there something I
> can do to make my script wait for a second or two? Like a pause or
> something?

sleep n

Where "n" is some number of a Float.

-- Thomas Adam

Jeff Miller

4/3/2008 12:16:00 AM

0

thanks!
--
Posted via http://www.ruby-....

Joshua Ballanco

4/3/2008 4:12:00 AM

0

Jeff Miller wrote:
> thanks!

Heh...this entire exchange should be put up on ruby-lang! It would be
the perfect advertisement:

"Imagine a programming language where the answers to simple questions
are not only simple, but you get 3 answers in under 15 minutes!"
--
Posted via http://www.ruby-....

Brendan Stennett

4/3/2008 5:43:00 AM

0

first = array[0]
last = array[-1]
--
Posted via http://www.ruby-....