Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Adding to an array question
Corey Haines
8/18/2007 7:42:00 PM
I need to add a string to the end of an array count number of times. I
was wondering if I could get some advice on the prefered way to do it
in Ruby.
@bucket is the array
def add_person(count, name)
@bucket += Array.new(count, name)
end
or some other ideas
def add_person(count, name)
count.times {@bucket << name }
end
# or some variation of looping
Personally, I like the adding a new array to the @bucket array. I
realize (at least I assume) that it creates a temporary Array, but I
think it is more understandable?
Thoughts?
Thanks, I'm just learning Ruby
-Corey
--
http://www.corey...
3 Answers
Stefan Rusterholz
8/19/2007 1:35:00 AM
0
Corey Haines wrote:
> I need to add a string to the end of an array count number of times. I
> was wondering if I could get some advice on the prefered way to do it
> in Ruby.
>
> @bucket is the array
>
>
> def add_person(count, name)
> @bucket += Array.new(count, name)
> end
>
> or some other ideas
>
> def add_person(count, name)
> count.times {@bucket << name }
> end
> # or some variation of looping
>
> Personally, I like the adding a new array to the @bucket array. I
> realize (at least I assume) that it creates a temporary Array, but I
> think it is more understandable?
>
> Thoughts?
> Thanks, I'm just learning Ruby
> -Corey
def add_person(name, count=1) # I assume adding once as a default is
rather safe
@bucket.concat(Array.new(count) {Â name.dup }) # a) this will create a
new string object everytime
@bucket.concat(Array.new(count, name) # b) this will use the *same*
string object everytime
end
Use either a) or b). Be aware that with b) if you do e.g.
@bucket.last.upcase!, it will reflect on all slots that contain the same
object.
As to why I prefer this concat solution over array +=: it doesn't create
throw away objects everytime.
Regards
Stefan
--
Posted via
http://www.ruby-...
.
Corey Haines
8/19/2007 2:39:00 PM
0
Thanks for the tips, Stefan. I like your tip on doing .dup, as well.
Thanks again!
-Corey
On 8/18/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
> Corey Haines wrote:
> > I need to add a string to the end of an array count number of times. I
> > was wondering if I could get some advice on the prefered way to do it
> > in Ruby.
> >
> > @bucket is the array
> >
> >
> > def add_person(count, name)
> > @bucket += Array.new(count, name)
> > end
> >
> > or some other ideas
> >
> > def add_person(count, name)
> > count.times {@bucket << name }
> > end
> > # or some variation of looping
> >
> > Personally, I like the adding a new array to the @bucket array. I
> > realize (at least I assume) that it creates a temporary Array, but I
> > think it is more understandable?
> >
> > Thoughts?
> > Thanks, I'm just learning Ruby
> > -Corey
>
> def add_person(name, count=1) # I assume adding once as a default is
> rather safe
> @bucket.concat(Array.new(count) {name.dup }) # a) this will create a
> new string object everytime
> @bucket.concat(Array.new(count, name) # b) this will use the *same*
> string object everytime
> end
>
> Use either a) or b). Be aware that with b) if you do e.g.
> @bucket.last.upcase!, it will reflect on all slots that contain the same
> object.
> As to why I prefer this concat solution over array +=: it doesn't create
> throw away objects everytime.
>
> Regards
> Stefan
> --
> Posted via
http://www.ruby-...
.
>
>
--
http://www.corey...
mlanza
8/20/2007 1:49:00 AM
0
2 cents:
count.times {@bucket << name }
Looks more readable to me.
Mario
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Adding to an array question
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password