Real Quick Question

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 M.

wait… sorry, my brain is tiered… array.first, array.last

Jeff M. 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)

On Wed, Apr 2, 2008 at 10:13 PM, Jeff M. [email protected]
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 :slight_smile:

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

On 02/04/2008, Jeff M. [email protected] 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 A.

sleep(1) # for 1 second

On 02/04/2008, Jeff M. [email protected] 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 A.

thanks!

Jeff M. 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!”

first = array[0]
last = array[-1]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs