[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

unroll array in initial vars

Krekna Mektek

1/3/2007 6:46:00 AM

Hi,

I'd like to know how I can unroll an array into some instance variables.

like this:

pseudocode:

class Person
def initialize
@name,@address,@place_of_birth,@whatever = Array
end
end

--* So I can call *--

person1 = Person.new(personal_data)

personal_data = ['Eric','Meanstreet 3','Buenos Aires','etc']

I hope it's clear what I'd like to know..

Krekna

5 Answers

Farrel Lifson

1/3/2007 6:50:00 AM

0

On 03/01/07, Krekna Mektek <krekna@gmail.com> wrote:
> Hi,
>
> I'd like to know how I can unroll an array into some instance variables.
>
> like this:
>
> pseudocode:
>
> class Person
> def initialize
> @name,@address,@place_of_birth,@whatever = Array
> end
> end
>
> --* So I can call *--
>
> person1 = Person.new(personal_data)
>
> personal_data = ['Eric','Meanstreet 3','Buenos Aires','etc']
>
> I hope it's clear what I'd like to know..
>
> Krekna
>
>

You do it exactly as you described

irb(main):001:0> personal_data = ['Eric','Meanstreet 3','Buenos Aires']
=> ["Eric", "Meanstreet 3", "Buenos Aires"]
irb(main):002:0> name,address,place_of_birth = personal_data
=> ["Eric", "Meanstreet 3", "Buenos Aires"]
irb(main):003:0> name
=> "Eric"
irb(main):004:0> address
=> "Meanstreet 3"
irb(main):005:0> place_of_birth
=> "Buenos Aires"
irb(main):006:0>

matt

1/3/2007 6:56:00 AM

0

On Wed, 2007-01-03 at 15:50 +0900, Farrel Lifson wrote:
> On 03/01/07, Krekna Mektek <krekna@gmail.com> wrote:
> > Hi,
> >
> > I'd like to know how I can unroll an array into some instance variables.
> >
> > like this:
> >
> > pseudocode:
> >
> > class Person
> > def initialize
> > @name,@address,@place_of_birth,@whatever = Array
> > end
> > end
> >
> > --* So I can call *--
> >
> > person1 = Person.new(personal_data)
> >
> > personal_data = ['Eric','Meanstreet 3','Buenos Aires','etc']
> >
> > I hope it's clear what I'd like to know..
> >
> > Krekna
> >
> >
>
> You do it exactly as you described
>
> irb(main):001:0> personal_data = ['Eric','Meanstreet 3','Buenos Aires']
> => ["Eric", "Meanstreet 3", "Buenos Aires"]
> irb(main):002:0> name,address,place_of_birth = personal_data
> => ["Eric", "Meanstreet 3", "Buenos Aires"]
> irb(main):003:0> name
> => "Eric"
> irb(main):004:0> address
> => "Meanstreet 3"
> irb(main):005:0> place_of_birth
> => "Buenos Aires"
> irb(main):006:0>
>

What if you had an array of arrays of persons? Something like:
[ ["name_1", "add_1", "birth_1"], ["name_2", "add_2", "birth_2"] ]

so that each person could be processed in a loop?




Robert Klemme

1/3/2007 10:24:00 AM

0

On 03.01.2007 07:46, Krekna Mektek wrote:
> I'd like to know how I can unroll an array into some instance variables.
>
> like this:
>
> pseudocode:
>
> class Person
> def initialize
> @name,@address,@place_of_birth,@whatever = Array
> end
> end
>
> --* So I can call *--
>
> person1 = Person.new(personal_data)
>
> personal_data = ['Eric','Meanstreet 3','Buenos Aires','etc']
>
> I hope it's clear what I'd like to know..

Not exactly. I'm irritated by the order of statements. You use
"personal_data" before you assigned to it.

If you just accidentally got the order wrong: you can do it as Farrel
pointed out. However, I would generally recommend having explicit
parameters. So, while you can do

def initialize(parms)
@name,@address,@place_of_birth,@whatever = parms
end

I would prefer

def initialize(name,address,place_of_birth,whatever)
@name = name
@address = address
@place_of_birth = place_of_birth
@whatever = whatever
end

It's safer and gives you automatic checking of argument lists - at least
Ruby checks the number of arguments for you. If you use the array
approach some or even all values may end up nil without you noticing it.

Kind regards

robert

Krekna Mektek

1/3/2007 1:45:00 PM

0

Good thinking indeed. But what if the parameter list is fixed? I think
in this case it does not matter, or does it? The order was indeed
wrong, ruby was irritated by it too so I changed it.

I've made the 2 dimensional array now.

Thanks for your help guys!

2007/1/3, Robert Klemme <shortcutter@googlemail.com>:
> On 03.01.2007 07:46, Krekna Mektek wrote:
> > I'd like to know how I can unroll an array into some instance variables.
> >
> > like this:
> >
> > pseudocode:
> >
> > class Person
> > def initialize
> > @name,@address,@place_of_birth,@whatever = Array
> > end
> > end
> >
> > --* So I can call *--
> >
> > person1 = Person.new(personal_data)
> >
> > personal_data = ['Eric','Meanstreet 3','Buenos Aires','etc']
> >
> > I hope it's clear what I'd like to know..
>
> Not exactly. I'm irritated by the order of statements. You use
> "personal_data" before you assigned to it.
>
> If you just accidentally got the order wrong: you can do it as Farrel
> pointed out. However, I would generally recommend having explicit
> parameters. So, while you can do
>
> def initialize(parms)
> @name,@address,@place_of_birth,@whatever = parms
> end
>
> I would prefer
>
> def initialize(name,address,place_of_birth,whatever)
> @name = name
> @address = address
> @place_of_birth = place_of_birth
> @whatever = whatever
> end
>
> It's safer and gives you automatic checking of argument lists - at least
> Ruby checks the number of arguments for you. If you use the array
> approach some or even all values may end up nil without you noticing it.
>
> Kind regards
>
> robert
>
>

Robert Klemme

1/3/2007 2:45:00 PM

0

On 03.01.2007 14:45, Krekna Mektek wrote:
> Good thinking indeed. But what if the parameter list is fixed? I think
> in this case it does not matter, or does it?

Err, what exactly do you mean by "fixed"? If you want to initialize
your members always to the same values you do not need arguments for
initialize at all.

If you just refer to the number and position of arguments, then this is
exactly the case where explicit parameters have the biggest advantage
just because of the number checking Ruby does.

Please try to be a bit more specific and thorough when you post. This
dramatically increases the likelihood of helpful answers.

> The order was indeed
> wrong, ruby was irritated by it too so I changed it.

:-)

robert