can't understand for String.delete

R

Ruby Newbee

Returns a copy of _str_ with all characters in the intersection of
its arguments deleted. Uses the same rules for building the set of
characters as +String#count+.

"hello".delete "l","lo" #=> "heo"
"hello".delete "lo" #=> "he"
"hello".delete "aeiou", "^e" #=> "hell"
"hello".delete "ej-m" #=> "ho"


What does this mean?
I can't understand for the rules.

Thanks.
 
J

Jesús Gabriel y Galán

=A0 =A0 Returns a copy of _str_ with all characters in the intersection o= f
=A0 =A0 its arguments deleted. Uses the same rules for building the set o= f
=A0 =A0 characters as +String#count+.

=A0 =A0 =A0 =A0"hello".delete "l","lo" =A0 =A0 =A0 =A0#=3D> "heo"
=A0 =A0 =A0 =A0"hello".delete "lo" =A0 =A0 =A0 =A0 =A0 =A0#=3D> "he"
=A0 =A0 =A0 =A0"hello".delete "aeiou", "^e" =A0 #=3D> "hell"
=A0 =A0 =A0 =A0"hello".delete "ej-m" =A0 =A0 =A0 =A0 =A0#=3D> "ho"


What does this mean?
I can't understand for the rules.

What it means is that it builds a character set performing the
intersection of all arguments to the method. So if you do
s.delete("h", "he") the intersection of the arguments is "h", because
it's the only one present in all arguments. If the arguments don't
intersect nothing is deleted:

irb(main):001:0> "hello".delete "h", "e"
=3D> "hello"

The other part of the explanation means that as the documentation for
String#count says
(http://ruby-doc.org/core/classes/String.html#M000834):

"Any other_str that starts with a caret (^) is negated. The sequence
c1=97c2 means all characters between c1 and c2. "

This means that s.delete("^aeiou") will remove all consonants and
leave the vocals, and that s.delete("a-m") will delete all characters
between "a" and "m":

irb(main):002:0> "hello".delete "a-m"
=3D> "o"
irb(main):003:0> "hello".delete "^aeiou"
=3D> "eo"
irb(main):004:0>

Hope this helps,

Jesus.
 
R

Ruby Newbee

ah~ thanks, I got it.
never saw this syntax in my other languages of programming, :)

 
J

Jesús Gabriel y Galán

ah~ thanks, I got it.
never saw this syntax in my other languages of programming, :)

"^" for negating and "-" for character ranges are usual syntax of
regular expressions.

Jesus.
 

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

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top