Rcr?

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 :slight_smile:
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 :wink:

Ty for tour thoughts.

Cheers
Robert

Hi,

In message “Re: RCR?”
on Fri, 4 May 2007 19:15:44 +0900, “Robert D.”
[email protected] writes:

|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?
I feel like explicit empty replacement describes user’s intention more
precisely than omitted default empty.

How do you think comparing those two:

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

          matz.

On Fri, May 04, 2007 at 07:15:44PM +0900, Robert D. 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 :wink:

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/

On Fri, 4 May 2007, Yukihiro M. wrote:

|instead.
How do you think comparing those two:

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

I must say that the latter looks quite intuitive to me: replace /s/ by
… um, well, nothing :slight_smile:
*t

On 5/4/07, Yukihiro M. [email protected] wrote:

|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 :slight_smile:

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 :wink:

Cheers
Robert

“Robert D.” [email protected] writes:

(a) Allow String#delete to have a Regex

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

On 5/4/07, Robert D. [email protected] 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 believe many who work a lot with .gsub and regexes
will want to have a “proper” String#delete as well :slight_smile:

On 5/4/07, Logan C. [email protected] wrote:

and of course the same thing for the ! variations of these methods

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 :wink:

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 :wink:

end

“abc”.without(“a”)
pretty cool

Robert

Logan C. 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?

On 5/4/07, Joel VanderWerf [email protected] wrote:

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

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Brian C. 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”

On 5/4/07, Rick DeNatale [email protected] wrote:

irb(main):005:0> “value1” + “value2”
=> “value1value2”
irb(main):006:0> “value1” + “value2” - “value2”
=> "1

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

That is a very valid point and makes me think about another
inconsistency
“acdb”.delete(“ab”) would be “cd” while
“acdb”.delete(/ab/) would be “acdb”!!

More I think about it more I believe that Logan hit a very sensible
point.

#delete(String) behaves as a hypotatical #delete([?c*]), this is a
little bit vague, let me give an example

“abcd”.delete(“ac”) —> “bd” why there is no “ac” in “abcd”
“abcd”.delete([?a, ?c]) --> “bd” of course!!!
The same can be applied to #without.
I feel that the following semantics would be pretty cool and
consistent, but would it justify a Change Request?
I will use PseufoRSpec synatx

(s=“abcd”).delete(“ab”).is “cd” && s.is “abcd”
(s=“abcd”).delete(“ac”).is “abcd” && s.is “abcd”
(s=“abcd”).delete!(“ab”).is “cd” && s.is “cd”
(s=“abcd”).delete!(“ac”).is “abcd” && s.is “abcd”
For all Strings s and Strings t {
s.delete(t).is s.delete(Regexp.escape(t))
s.delete!(t).is s.delete!(Regexp.escape(t))
s.delete_first(t).is s.delete_first(Regexp.escape(t))
s.delete_first!(t).is s.delete_first!(Regexp.escape(t))
s.delete_last(t).is s.delete_last(Regexp.escape(t))
s.delete_last!(t).is s.delete_last!(Regexp.escape(t))
}
(s=“abxab”).delete(/ab/).is “x” && s.is “abxab”
(s=“ab”).delete(/./).is.empty && s.is “ab”
(s=“ab”).delete!(/./).is.empty && s.is.empty

I guess that the semantics of delete_first* and delete_last* are
pretty trivial, so I leave them out.

Now for without
“ab”.without(“ab”).shall.raise ArgumentError # But maybe we shall be
duckier here?(1)
“ab”.without!(“ab”).shall.raise ArgumentError# But maybe we shall be
duckier here?
“abcd”.without(/./).shall.raise ArgumentError
“abcd”.without!(/./).shall.raise ArgumentError
(s=“abce”).without([?a,?c]).is “be” && s.is “abce”
(s=“abce”).without!([?a,?c]).is “be” && s.is “be”

And eventually
“abcd”.to_a.is [?a,?b,?c,?d]
“aba”.to_a.is [?a, ?b, ?a]
“abcd”.to_set.is [?a, ?b].to_set

Concerning (1):
One could also imagine
class String
def without args
args = args.to_set # but that invalidates some specs above
# I have mixed feelings about
this, it is “duckier” sure
# but it exactly brings up
what Rick has talked about, we are not applying #without to the
string but to the characters of the string, better to make that
crystal clear in the API.

Hopefully someone still reads that whole lot :wink:

Cheers
Robert

Just for the fun let us imagine the following setup, sorry I am
striving away from
String#- to the original topic. But I do not consider the deviation of
String#- off topic it is closely related.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs