Remove ends of array and interleave

Greetings:

I have some problems getting a couple of elements working right… I
have 2 small issues:

  1. Trying to remove the first and last element in an array in Ruby:
    def remove_ends(a)
    a.pop
    a.shift
    end

I need out put like:
Input array: [1,2,3,4]
Output: [2,3]

This seems like I’m stumbling on how to reference the array that is
being processed rather than the original??

  1. I’m trying to interleave 2 arrays. I’ve seen some examples online but
    I need to keep the arguments (a,b) and other examples don’t have 2
    inputs.
    I’m pretty close:

a.zip(b)
join

It interleaves just fine… but I need the output to be 1 string:
“a1b2c3d4” I’m not sure how to get to the array created to use JOIN.

Thanks

On Wed, Dec 14, 2011 at 3:42 AM, matha wasint [email protected]
wrote:

I need out put like:
Input array: [1,2,3,4]
Output: [2,3]

This seems like I’m stumbling on how to reference the array that is
being processed rather than the original??

What exactly is your problem?

  1. I’m trying to interleave 2 arrays. I’ve seen some examples online but
    I need to keep the arguments (a,b) and other examples don’t have 2
    inputs.
    I’m pretty close:

a.zip(b)
join

It interleaves just fine… but I need the output to be 1 string:
“a1b2c3d4” I’m not sure how to get to the array created to use JOIN.

What’s wrong with a.zip(b).join ?

Cheers

rober

  1. Trying to remove the first and last element in an array in Ruby:
    def remove_ends(a)
    a.pop
    a.shift
    end

Is your problem, that a gets altered?
If so, try
def remove_ends(a)
a[1…-2]
end

  1. I’m trying to interleave 2 arrays. I’ve seen some examples online but

a = [1,2,3]
b = [‘a’,‘b’,‘c’]

a.zip(b).join # => “1a2b3c”

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