Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Think I need an Array#split
Bertram Scharpf
1/5/2005 1:04:00 AM
Hi,
wouldn't it be a good idea to have a method doing this:
['a','b','','c','d','','e'].split ''
#=> [['a','b'],['c','d'],['e']]
I wrote this method for my program in Ruby, but if you were
interested in integrating it into the language, I would
like to contribute it to `array.c'.
I made both solutions available at
<
http://projects.bertram-scharpf.de/tmp/arraysplit....
.
Bertram
P.S.: For those who look at the C code: is it OK not to
destroy the last `cur' object?
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...
3 Answers
leon breedt
1/5/2005 1:28:00 AM
0
On Wed, 5 Jan 2005 10:04:08 +0900, Bertram Scharpf
<lists@bertram-scharpf.de> wrote:
> I made both solutions available at
> <
http://projects.bertram-scharpf.de/tmp/arraysplit....
.
I had a look, perhaps it could emulate String#split by performing an
"awk split" (on whitespace) if there is no delimiter argument given?
Also, allowing a Regexp as a
delimiter would further emulate the String#split functionality.
> P.S.: For those who look at the C code: is it OK not to
> destroy the last `cur' object?
Its fine, Ruby uses a mark-and-sweep GC, so unless you've explicitly
marked 'cur' yourself (pushing it onto another array will cause it to
get marked automatically), it will get destroyed at the next GC run.
Regards
Leon
Robert Klemme
1/5/2005 5:28:00 PM
0
"leon breedt" <bitserf@gmail.com> schrieb im Newsbeitrag
news:270bd0c40501041728ba09e80@mail.gmail.com...
> On Wed, 5 Jan 2005 10:04:08 +0900, Bertram Scharpf
> <lists@bertram-scharpf.de> wrote:
>> I made both solutions available at
>> <
http://projects.bertram-scharpf.de/tmp/arraysplit....
.
> I had a look, perhaps it could emulate String#split by performing an
> "awk split" (on whitespace) if there is no delimiter argument given?
> Also, allowing a Regexp as a
> delimiter would further emulate the String#split functionality.
Another option would be an optional block that evaluates to true for
delimiters. I didn't look at the proposed implementation, but here's how
I'd do it:
module Enumerable
def split(delim = /\A\s*\z/, &b)
b ||= lambda {|x| delim === x}
inject([[]]) do |ar, x|
if b.call x
ar << []
else
ar[-1] << x
end
ar
end
end
end
Kind regards
robert
Bertram Scharpf
1/6/2005 9:23:00 PM
0
Am Donnerstag, 06. Jan 2005, 02:31:31 +0900 schrieb Robert Klemme:
>
> "leon breedt" <bitserf@gmail.com> schrieb im Newsbeitrag
> news:270bd0c40501041728ba09e80@mail.gmail.com...
> >On Wed, 5 Jan 2005 10:04:08 +0900, Bertram Scharpf
> ><lists@bertram-scharpf.de> wrote:
> >>I made both solutions available at
> >><
http://projects.bertram-scharpf.de/tmp/arraysplit....
.
> >I had a look, perhaps it could emulate String#split by performing an
> >"awk split" (on whitespace) if there is no delimiter argument given?
> >Also, allowing a Regexp as a
> >delimiter would further emulate the String#split functionality.
>
> Another option would be an optional block that evaluates to true for
> delimiters. I didn't look at the proposed implementation, but here's how
Very nice ideas, indeed. I widened my function.
<
http://projects.bertram-scharpf.de/tmp/arraysplit....
Is it now worth to become a part of Ruby?
Bertram
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Think I need an Array#split
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