RCR?

R

Robert Dober

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
 
B

Brian Candler

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/
 
R

Robert Dober

Hi,

In message "Re: RCR?"

|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.

Could you tell me why you want to reduce tiny three letters?
No I cannot not and even if I could I would not because I do not feel
qualified to do some
lobbying. I just like it, and if others do not I will not talk about it anymore.
And you are of course one of the most important others.
I feel like explicit empty replacement describes user's intention more
precisely than omitted default empty.
And the implicit parameter is dangerous as it might mask errors where
the user just forgot the second parameter.
The importance of the replacement string might as well not justify that danger.
How do you think comparing those two:

"some string".gsub(/s/,"")
"some string".gsub(/s/)

I just love the second, but I see the dangers and problems too.
I just need to communicate to see beyond things. Thank you a lot of
wasting your time in order to allow me to do that.

I guess I always learn a lot from my intentional RCRs :)

There would be another road to talk of course but it will brake to much code.

String#delete_all( str_or_rgx )
String#delete_first( str_or_rgx)
String#delete_last(str_or_rgx) # (1)
String#delete an alias to String#delete_all
and of course the same thing for the ! variations of these methods

(1) would it not be nice to get rid of the
"robert is stupid, robert created ruby".reverse.sub("trebor", "ztam").reverse
idiom ;)

Cheers
Robert
 
M

Marc Heiler

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

Robert Dober

Getting OT, here but am I the only fool who constantly forgets that
String#delete is _not_ destructive like (Array|Hash)#delete_at ?
No I checked in Rdoc and double checked in irb before writing this.
But I feel that it is good as it is.
Now to answer your question, neither you nor I are the only fools ;)
I proceed to fix my problem:

class String
alias without delete
which is worth a serious consideration!!! I think it is very readable

what about

without_all (aliased to without)
without_first
without_last
and
delete_all!
delete_first!
delete_last!
that would definitely make things easier for us fools ;)

"abc".without("a") pretty cool

Robert
 
J

Joel VanderWerf

Logan Capaldo wrote:
...
Getting OT, here but am I the only fool who constantly forgets that
String#delete is _not_ destructive like (Array|Hash)#delete_at ?

I proceed to fix my problem:

class String
alias without delete
end

"abc".without("a")

I like that. I keep having to check in irb or ri whether String#delete
is destructive, even after 6+ years. Or what about

class String
alias - delete
end

"foobar" - "oa" # => "fbr"

Using an operator makes it abundantly clear that neither string is being
modified. But then of course you don't get arity >= 2 version of delete.

Was String#- being reserved for something else?
 
J

Joel VanderWerf

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"
 
R

Rick DeNatale

class String
alias - delete
end

"foobar" - "oa" # => "fbr"

Using an operator makes it abundantly clear that neither string is being
modified. But then of course you don't get arity >= 2 version of delete.

Was String#- being reserved for something else?

The thing that vaguely bothers me about this definition of String#- is
that it doesn't seem ot have the same relationship with String#+ that
+ and minus usually have.

irb(main):002:0> class String
irb(main):003:1> alias - delete
irb(main):004:1> end
=> nil
irb(main):005:0> "value1" + "value2"
=> "value1value2"
irb(main):006:0> "value1" + "value2" - "value2"
=> "1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

RCR 13 0
regex woes with breaklines 3
Looking For Advice 1
Chatbot 0
Search Results with Pagination 1
RCR: New interpreter switch 3
gsub ? 2
rcr: request for __DATA__ besides __END__ .. 0

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top