Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Receiving array naturally?

Reply
Thread Tools

Receiving array naturally?

 
 
Terry Michaels
Guest
Posts: n/a
 
      09-08-2010
As I learn Ruby, I find a lot of flexibility in the syntax. I was
thinking there was a way to do something like so: Say I had a method
that would receive an array. Is it possible to define the method so that
it can take the array elements /either/ as an Array object, /or/ as a
comma-separated (variable) list of elements (without the brackets)?

Like,

process_data(string1, string2, string3, ...)

would be the same as

string_array = [string1, string2, string3, ...]
process_data(string_array)

I thought something like so would work:

def process_data(*strings)
...
end

That works with the comma-separated list, because the list gets combined
into one array ("strings"), but if I pass in an array object, that array
object is not split up, but rather becomes one (Array object) element of
the "strings" array.

Do I need to write code that checks whether each object passed in is an
array or not, manually splitting if necessary (which perhaps is
complicated) or is there something else I'm overlooking? Or perhaps is
my whole idea dumb from the start...?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      09-08-2010
On Wed, Sep 8, 2010 at 7:10 AM, Terry Michaels <> wrote=
:
> As I learn Ruby, I find a lot of flexibility in the syntax. I was
> thinking there was a way to do something like so: Say I had a method
> that would receive an array. Is it possible to define the method so that
> it can take the array elements /either/ as an Array object, /or/ as a
> comma-separated (variable) list of elements (without the brackets)?
>
> Like,
>
> =A0process_data(string1, string2, string3, ...)
>
> would be the same as
>
> =A0string_array =3D [string1, string2, string3, ...]
> =A0process_data(string_array)
>
> I thought something like so would work:
>
> =A0def process_data(*strings)
> =A0 =A0 ...
> =A0end
>
> That works with the comma-separated list, because the list gets combined
> into one array ("strings"), but if I pass in an array object, that array
> object is not split up, but rather becomes one (Array object) element of
> the "strings" array.
>
> Do I need to write code that checks whether each object passed in is an
> array or not, manually splitting if necessary (which perhaps is
> complicated) or is there something else I'm overlooking? Or perhaps is
> my whole idea dumb from the start...?


If in the caller you know whether it's an array or not you can use the
splat when calling the method:

array =3D %w{a b c d e}
process_data(*array)

keeping the definition as
def process_data(*strings)

Don't know if this is good enough for your use case or not.

Jesus.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-08-2010
On Wed, Sep 8, 2010 at 7:10 AM, Terry Michaels <> wrote=
:
> As I learn Ruby, I find a lot of flexibility in the syntax. I was
> thinking there was a way to do something like so: Say I had a method
> that would receive an array. Is it possible to define the method so that
> it can take the array elements /either/ as an Array object, /or/ as a
> comma-separated (variable) list of elements (without the brackets)?
>
> Like,
>
> =A0process_data(string1, string2, string3, ...)
>
> would be the same as
>
> =A0string_array =3D [string1, string2, string3, ...]
> =A0process_data(string_array)
>
> I thought something like so would work:
>
> =A0def process_data(*strings)
> =A0 =A0 ...
> =A0end
>
> That works with the comma-separated list, because the list gets combined
> into one array ("strings"), but if I pass in an array object, that array
> object is not split up, but rather becomes one (Array object) element of
> the "strings" array.
>
> Do I need to write code that checks whether each object passed in is an
> array or not, manually splitting if necessary (which perhaps is
> complicated) or is there something else I'm overlooking? Or perhaps is
> my whole idea dumb from the start...?


I wouldn't judge on this one although I generally believe that
reduction of alternatives increases clarity.

You could do

def simple(*a)
a.flatten.each do |x|
x.whatever
end
end

def complex(*a)
if a.length =3D=3D 1 && Enumerable =3D=3D=3D a.first
a.first
else
a
end.each do |x|
x.whatever
end
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Ammar Ali
Guest
Posts: n/a
 
      09-08-2010
On Wed, Sep 8, 2010 at 10:09 AM, Robert Klemme
<> wrote:
> You could do
>
> def simple(*a)
> =C2=A0a.flatten.each do |x|
> =C2=A0 =C2=A0x.whatever
> =C2=A0end
> end


It's also possible to use * when passing the array to the method,
without changing the method:

> some_items =3D [1, "more", %w{four six eight}]
> some_method *some_items


Regards,
Ammar

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-08-2010
On Wed, Sep 8, 2010 at 11:36 AM, Ammar Ali <> wrote:
> On Wed, Sep 8, 2010 at 10:09 AM, Robert Klemme
> <> wrote:
>> You could do
>>
>> def simple(*a)
>> =A0a.flatten.each do |x|
>> =A0 =A0x.whatever
>> =A0end
>> end

>
> It's also possible to use * when passing the array to the method,
> without changing the method:
>
>> some_items =3D [1, "more", %w{four six eight}]
>> some_method *some_items


Of course, but that's not what Terry asked for. He wanted to make it
convenient for the caller so he could do

m(1,2,3,4)
m([1,2,3,4])
m(some_array)

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Terry Michaels
Guest
Posts: n/a
 
      09-09-2010
Clifford Heath wrote:
> Terry Michaels wrote:
>> def process_data(*strings)
>> ...
>> end
>>
>> That works with the comma-separated list, because the list gets combined
>> into one array ("strings"), but if I pass in an array object, that array
>> object is not split up, but rather becomes one (Array object) element of
>> the "strings" array.
>>
>> is there something else I'm overlooking?

>
> You've overlooked the simplest way:
>
> def process_data(*strings)
> Array(strings).each do |s|
> ...
> end
> end


That doesn't work, because an array passed it gets treated like simply
an element of the "strings" array:

def process_data(*strings)
Array(strings).each do |s|
print s.class.to_s + " " + s.to_s + " " + "\n"
end
end

process_data("a", "b", "c")
process_data(["a", "b", "c"])

# Produces:
#
# String a
# String b
# String c
# Array abc
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      09-09-2010
On Wed, Sep 8, 2010 at 1:10 PM, Terry Michaels <> wrote=
:
> =A0def process_data(*strings)
> =A0 =A0 ...
> =A0end


yes

> Do I need to write code that checks whether each object passed in is an a=

rray or not

yes

>, manually splitting if necessary (which perhaps is
> complicated) or is there something else I'm overlooking? Or perhaps is
> my whole idea dumb from the start...?


no

starting fr your splat technique...
you just need to adjust a little bit...


eg,

>> def m *a
>> a =3D a.first if a.first.is_a? Array
>> a
>> end

=3D> nil
>> m 1,2

=3D> [1, 2]
>> m [1,2]

=3D> [1, 2]
>> m [1,2,"a",[3,4]]

=3D> [1, 2, "a", [3, 4]]
>> m 1,2,"a",[3,4]

=3D> [1, 2, "a", [3, 4]]
>>


welcome to the wonderful world of ruby.
best regards -botp

 
Reply With Quote
 
Clifford Heath
Guest
Posts: n/a
 
      09-09-2010
Terry Michaels wrote:
> def process_data(*strings)
> ...
> end
>
> That works with the comma-separated list, because the list gets combined
> into one array ("strings"), but if I pass in an array object, that array
> object is not split up, but rather becomes one (Array object) element of
> the "strings" array.
>
> is there something else I'm overlooking?


You've overlooked the simplest way:

def process_data(*strings)
Array(strings).each do |s|
...
end
end
 
Reply With Quote
 
Clifford Heath
Guest
Posts: n/a
 
      09-09-2010
Terry Michaels wrote:
> Clifford Heath wrote:
>> You've overlooked the simplest way:

> That doesn't work, because an array passed it gets treated like simply
> an element of the "strings" array:


Ah, right you are. I was thinking of this:

Array([2]) => [2]
Array(2) => [2]
but Array(*[1,2,3]) => ArgumentError: wrong number of arguments (3 for 1)

.... and yes, I left out the *.
What I was thinking of was Array[*strings]

However (I knew I'd done something like this before!):

def process_data(*strings)
strings = *strings
strings.each do |s|
print s.class.to_s + " " + s.to_s + " " + "\n"
end
end

process_data("a", "b", "c")
process_data(["a", "b", "c"])

Does what you want.

Clifford Heath.
 
Reply With Quote
 
Terry Michaels
Guest
Posts: n/a
 
      09-10-2010
Clifford Heath wrote:
> Terry Michaels wrote:
>> Clifford Heath wrote:
>>> You've overlooked the simplest way:

>> That doesn't work, because an array passed it gets treated like simply
>> an element of the "strings" array:

>
> Ah, right you are. I was thinking of this:
>
> Array([2]) => [2]
> Array(2) => [2]
> but Array(*[1,2,3]) => ArgumentError: wrong number of arguments (3 for
> 1)
>
> ... and yes, I left out the *.
> What I was thinking of was Array[*strings]
>
> However (I knew I'd done something like this before!):
>
> def process_data(*strings)
> strings = *strings
> strings.each do |s|
> print s.class.to_s + " " + s.to_s + " " + "\n"
> end
> end
>
> process_data("a", "b", "c")
> process_data(["a", "b", "c"])
>
> Does what you want.
>
> Clifford Heath.


This seems to work.

If I might ask, what exactly is the line

strings = *strings

doing?

For that matter, I could use a little more clarification on what the '*'
(glob?) operator does...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Receiving E-Mail Problem Fat Cat Firefox 2 07-02-2005 11:30 PM
Receiving IP address from Router but NO Internet Connection =?Utf-8?B?SmFzb24gVi4=?= Wireless Networking 2 01-09-2005 08:33 AM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57