Any one know why Array.join can’t take a code block and join that
value instead of what is actually in the array? Perhaps I want to
join on a subfield or process the item before joining it. Right now
I use collect and then join, but that just seems wasteful of memory.
for instance (and yes I know it is a silly example):
list = [“A”, “B”, “C”]
#I want to do this
puts list.join(","){|item| item.downcase}
#but I have to do this
puts list.collect{|item| item.downcase}.join(",")
Am I missing something?
Thanks,
Walt
···
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
No, that appears to be a very sensible solution to me. It
communicates your intentions exactly. If you are working on a very
large array, then perhaps you might need to find a way to conserve
memory, but it shouldn’t be necessary in 99% of cases: the memory used
by #collect is very temporary and will be garbage collected.
That said, the usage of #join you propose doesn’t sound too bad at
all.
Gavin
···
On Friday, March 28, 2003, 9:31:47 AM, walter wrote:
Any one know why Array.join can’t take a code block and join that
value instead of what is actually in the array? Perhaps I want to
join on a subfield or process the item before joining it. Right now
I use collect and then join, but that just seems wasteful of memory.
for instance (and yes I know it is a silly example):
list = [“A”, “B”, “C”]
#I want to do this
puts list.join(“,”){|item| item.downcase}
#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)
I think this is an excellent idea and would be a nice addition to Ruby.
Please RCR it, if not done already.
— url: What is Pre-settlement Funding?
author: Bruce R. Williams
url: http://codedbliss.com
irc: { nick: iusris, channel: ruby-lang, server: irc.freenode.net }
quote: >
It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.
– Samuel Adams
···
On Thursday 27 March 2003 05:38 pm, Gavin Sinclair wrote:
On Friday, March 28, 2003, 9:31:47 AM, walter wrote:
Any one know why Array.join can’t take a code block and join that
value instead of what is actually in the array? Perhaps I want to
join on a subfield or process the item before joining it. Right now
I use collect and then join, but that just seems wasteful of memory.
for instance (and yes I know it is a silly example):
list = [“A”, “B”, “C”]
#I want to do this
puts list.join(“,”){|item| item.downcase}
#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)
Am I missing something?
No, that appears to be a very sensible solution to me. It
communicates your intentions exactly. If you are working on a very
large array, then perhaps you might need to find a way to conserve
memory, but it shouldn’t be necessary in 99% of cases: the memory used
by #collect is very temporary and will be garbage collected.
That said, the usage of #join you propose doesn’t sound too bad at
all.
Uh, no it doesn’t strike me as what ‘join’ is supposed to do.
Intuitively, I think that a ‘join’ function should only join the entries
of the array and not modify the entries themselves.
···
On Fri, Mar 28, 2003 at 06:00:59PM +0900, Yukihiro Matsumoto wrote:
#I want to do this
puts list.join(“,”){|item| item.downcase}
#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)
Hmm, I feel like this filtering is not part of “join” operation.
But I’m interested in how others think.
matz.
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
depone: depone (di-POHN) verb tr., intr.
To declare under oath.
“Yukihiro Matsumoto” matz@ruby-lang.org schrieb im Newsbeitrag
news:1048842051.937425.25237.nullmailer@picachu.netlab.jp…
Hi,
#I want to do this
puts list.join(“,”){|item| item.downcase}
#but I have to do this
puts list.collect{|item| item.downcase}.join(“,”)
Hmm, I feel like this filtering is not part of “join” operation.
But I’m interested in how others think.
I agree. People who need this can easily implement it themselves (even
for Array and other built in classes) but it should definitely not be part
of the standard library.
I would agree - otherwise you’re combining the functionality of ‘join’ with
‘collect’. What might be useful is for join to take a block which decides
what separator to use based on the value of the previous item, eg:
(1…10).to_a.join { |i|
i % 2 == 0 ? " " : “-”
} # => “1-2 3-4 5-6 7-8 9-10”
This is a trivial example, and I can’t think of a real example off the top of
my head, so feel free to reject it on the grounds that it may not be useful,
but this just occurred to me and I thought I’d throw it out there…
Tim Bates
···
On Fri, 28 Mar 2003 7:37 pm, Daniel Carrera wrote:
Uh, no it doesn’t strike me as what ‘join’ is supposed to do.
Intuitively, I think that a ‘join’ function should only join the entries
of the array and not modify the entries themselves.
Uh, no it doesn’t strike me as what ‘join’ is supposed to do.
Intuitively, I think that a ‘join’ function should only join the entries
of the array and not modify the entries themselves.
Uh, no it doesn’t strike me as what ‘join’ is supposed to do.
Intuitively, I think that a ‘join’ function should only join the entries
of the array and not modify the entries themselves.
I would agree - otherwise you’re combining the functionality of ‘join’ with
‘collect’. What might be useful is for join to take a block which decides
what separator to use based on the value of the previous item,
That’d be cool.
eg:
(1…10).to_a.join { |i|
i % 2 == 0 ? " " : “-”
} # => “1-2 3-4 5-6 7-8 9-10”
This is a trivial example, and I can’t think of a real example off the top of
my head,
I don’t have any use for this yet, too, but you could even pass both
the last and the next item to the block.
Regards,
Pit
···
On 28 Mar 2003 at 18:14, Tim Bates wrote:
What might be useful is for join to take a block which decides
what separator to use based on the value of the previous item, eg:
(1…10).to_a.join { |i|
i % 2 == 0 ? " " : “-”
} # => “1-2 3-4 5-6 7-8 9-10”
This is a trivial example, and I can’t think of a real example off the top of
my head, so feel free to reject it on the grounds that it may not be useful,
but this just occurred to me and I thought I’d throw it out there…
Ooops. Cancel that. Stupid example. I just realized that it doesn’t use
‘i’ at all. I’m sure that there’s a good use for this feature somewhere
though.
···
On Fri, Mar 28, 2003 at 06:27:27PM +0900, Daniel Carrera wrote:
out of curiosity, why would a block to #join be awkward? It’s wholly
additive to the library, it doesn’t break any existing code, and
anything you write could just as easily not use it.
It seems a rather invisible useful addition to me.
out of curiosity, why would a block to #join be awkward? It’s wholly
additive to the library, it doesn’t break any existing code, and
anything you write could just as easily not use it.
To me, it’s not obvious that join with a block should do collect-and-join.
Why not select-and-join, for example?
Regards,
Brian.
···
On Sat, Mar 29, 2003 at 12:50:01AM +0900, Michael Campbell wrote:
out of curiosity, why would a block to #join be awkward? It’s wholly
additive to the library, it doesn’t break any existing code, and
anything you write could just as easily not use it.
It seems a rather invisible useful addition to me.