[lnkForumImage]
TotalShareware - Download Free Software

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


 

Robert Dober

5/4/2007 10:16:00 AM

Hi list

I'd like to have your feedback on yet another RCR idea I had.

Currently I am writing lots of code like this:

"some string".gsub(/s/,"") # I am using more complex rgxs therefore
String#delete is not
# an option.

I would like to write
"some string".gsub(/s/)
instead.

There are two roads to walk:
(a) Allow String#delete to have a Regex
(b) Give String#gsub, String#sub, String#gsub! and String#sub! the
empty string as default for parameter replacement.

(a) "abcdefgh".delete(/[aeiou]/)
For: It is concise syntax, and does very nicely what the method name
says, however...
Against: does it delete the first match or all matches? I'd say all to
be consistent with
the classical behavior of #delete, to replace only the first match
would be a very bad inconsistency.
Therefore we would have the semantics of gsub(...,"") and sub(...,"")
would not be available.
I do not like it.

(b) "abcdef".gsub(/[aeiou]/) and "ijklmnop".sub(/[aeiou]/) and the !
variants of course.
For: I like it :)
I feel that it is very Rubyish
Should be trivial to implement.
Againts: Well it is a change request for very little functionality,
but if enough people like it and if Matz likes it ;)

Ty for tour thoughts.

Cheers
Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

4 Answers

Brian Candler

5/4/2007 10:36:00 AM

0

On Fri, May 04, 2007 at 07:15:44PM +0900, Robert Dober wrote:
> Currently I am writing lots of code like this:
>
> "some string".gsub(/s/,"") # I am using more complex rgxs therefore
> String#delete is not
> # an option.
>
> I would like to write
> "some string".gsub(/s/)
> instead.
...
> Againts: Well it is a change request for very little functionality,
> but if enough people like it and if Matz likes it ;)

This is perhaps a good example of where making your own extensions to
built-in classes, to make your own domain-specific language, is a good idea.
That is, if you're doing it so many times that ,"" is getting hard to type,
then why not shrink 'gsub' too while you're at it?

class String
def -(re)
gsub(re,'')
end
end

a = "some string"
p a - /s/

Christian Neukirchen

5/4/2007 1:43:00 PM

0

"Robert Dober" <robert.dober@gmail.com> writes:

> (a) Allow String#delete to have a Regex

I've been wanting that for ages and it's the best solution.

> Cheers
> Robert
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

Marc Heiler

5/4/2007 1:48:00 PM

0

I believe many who work a lot with .gsub and regexes
will want to have a "proper" String#delete as well :-)


--
Posted via http://www.ruby-....

Joel VanderWerf

5/4/2007 6:52:00 PM

0

Brian Candler wrote:
...
> class String
> def -(re)
> gsub(re,'')
> end
> end
>
> a = "some string"
> p a - /s/

That's better than aliasing - to delete, since the latter functionality
can always be recovered with /[ ... ]/, and it adds the flexibility to
do things like:

"the end" - "the"

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407