array

V

Vetrivel Vetrivel

How to Join multiple arrays in a single method in ruby
Ex:
a= [ 1 ,2 , 3]
b = [4,5,6]
c = [7,8,9]
Using single method i want b and c array content into the a array.
 
A

Ashikali Ashikali

Vetrivel said:
How to Join multiple arrays in a single method in ruby
Ex:
a= [ 1 ,2 , 3]
b = [4,5,6]
c = [7,8,9]
Using single method i want b and c array content into the a array.

Hey , what is this ? are you read documentation for ruby or any thing
else ?.

Answer is ,
a + b + c
 
V

Vetrivel Vetrivel

Ashikali said:
Vetrivel said:
How to Join multiple arrays in a single method in ruby
Ex:
a= [ 1 ,2 , 3]
b = [4,5,6]
c = [7,8,9]
Using single method i want b and c array content into the a array.

Hey , what is this ? are you read documentation for ruby or any thing
else ?.

Answer is ,
a + b + c

We can do this .In ruby The '+' is method.You are calling '+' method
three times.But I have asked using single function we have to do.
 
B

Brian Adkins

Ashikali Ashikali said:
Vetrivel said:
How to Join multiple arrays in a single method in ruby
Ex:
a= [ 1 ,2 , 3]
b = [4,5,6]
c = [7,8,9]
Using single method i want b and c array content into the a array.

Hey , what is this ? are you read documentation for ruby or any thing
else ?.

Answer is ,
a + b + c

I expect that was what the OP was looking for. But on the other hand,
your solution involves two methods, not one. One way to do it with one
method is the following:

require 'pp'

class Array
def concat_multi! *lists
lists.each {|list| self.concat(list) }
end
end

a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
d = [10, 11]

a.concat_multi!(b, c, d)

pp a
 
L

lasitha

We can do this .In ruby The '+' is method.You are calling '+' method
three times.But I have asked using single function we have to do.

Why? Is this a quiz question? Please explain why it matters that
it's a single method call.

Does this work for you?:
[a, b, c].flatten

Cheers,
lasitha
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top