Real Quick Question...

J

Jeff Miller

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
 
J

Joel VanderWerf

Jeff said:
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)
 
J

Jano Svitok

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 :)
 
T

Thomas Adam

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
 
J

Jeff Miller

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
 
T

Thomas Adam

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
 
J

Joshua Ballanco

Jeff said:

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!"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top