[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: RCR?

Yukihiro Matsumoto

5/4/2007 10:24:00 AM

Hi,

In message "Re: RCR?"
on Fri, 4 May 2007 19:15:44 +0900, "Robert Dober" <robert.dober@gmail.com> 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.

6 Answers

Robert Dober

5/4/2007 11:46:00 AM

0

On 5/4/07, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> Hi,
>
> In message "Re: RCR?"
> on Fri, 4 May 2007 19:15:44 +0900, "Robert Dober" <robert.dober@gmail.com> 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?
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
>
> matz.
>
>


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

Tomas Pospisek

5/4/2007 11:46:00 AM

0

Robert Dober

5/4/2007 1:52:00 PM

0

On 5/4/07, Logan Capaldo <logancapaldo@gmail.com> wrote:
> On 5/4/07, Robert Dober <robert.dober@gmail.com> wrote:
> >
> >
> > 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
> >
> >
>
> 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 ;)


> end

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

Robert

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

Joel VanderWerf

5/4/2007 6:46:00 PM

0

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?

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

Rick DeNatale

5/4/2007 7:39:00 PM

0

On 5/4/07, Joel VanderWerf <vjoel@path.berkeley.edu> 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.denh...

Robert Dober

5/4/2007 8:35:00 PM

0

On 5/4/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On 5/4/07, Joel VanderWerf <vjoel@path.berkeley.edu> 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.denh...
>
>

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 ;)

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.




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