suggested changes for ruby libraries

J

jdm

These are all relative to version 1.8.1.
Don't know if any of these (have been)|(will be) incorporated in later versions.

String.delete() currently takes only a String for an argument (albeit with
limited regex functionality such as ^ and c1-c2). It would be useful, and
orthogonal, to take a Regexp as an argument too (orthogonal in that most of the
other String methods involving the specification/identification/location of a
string take both String and Regexp arguments).

Same comment as above for Array.delete().

When String.index() is given a multi-character String argument, it currently
returns the index of the first occurrence of the *first* character of the
argument. It would be useful to add an argument to tell it to return the index
of the *last* character of the argument (this new argument should default to
FIRST). For example:
"hello".index("lo",FIRST) -> 3
"hello".index("lo",LAST) -> 4

A similar argument could be applied to String.rindex().

When supplied with two Fixnums, a Range, or a Rexexp, String.[] returns a
String. But when supplied with a single Fixnum, it returns a Fixnum. For
example:
a="hello"
a[2,2] -> "ll"
a[1..3] -> "ell"
a[1] -> 101
This behavior is very non-orthogonal (otherwise worded as "violates the
Principle of Least Surprise"). String[Fixnum] should return a single character
string as follows:
a[1] -> "e"
 
A

Austin Ziegler

These are all relative to version 1.8.1. Don't know if any of these
(have been)|(will be) incorporated in later versions.
String.delete() currently takes only a String for an argument (albeit
with limited regex functionality such as ^ and c1-c2). It would be
useful, and orthogonal, to take a Regexp as an argument too
(orthogonal in that most of the other String methods involving the
specification/identification/location of a string take both String and
Regexp arguments).

I think that #gsub/#gsub! is the preferred idiom here for regex, e.g.:

"foo".gsub!(/o/, '')
Same comment as above for Array.delete().

%w(foo bar boo baz moo).delete_if { |el| el =3D~ /o/ }
When String.index() is given a multi-character String argument, it
currently returns the index of the first occurrence of the *first*
character of the argument. It would be useful to add an argument to
tell it to return the index of the *last* character of the argument
(this new argument should default to FIRST). For example:
"hello".index("lo",FIRST) -> 3
"hello".index("lo",LAST) -> 4

A similar argument could be applied to String.rindex().

I do not believe that this necessary.

# Equivalent to your ...LAST suggestion
"hello".index("lo") + ("lo".size - 1)
When supplied with two Fixnums, a Range, or a Rexexp, String.[]
returns a String. But when supplied with a single Fixnum, it returns a
Fixnum. For example:

a=3D"hello"
a[2,2] -> "ll"
a[1..3] -> "ell"
a[1] -> 101

This behavior is very non-orthogonal (otherwise worded as "violates
the Principle of Least Surprise"). String[Fixnum] should return a
single character string as follows:

It is highly recommended that you not mention said principle. At best,
the principle is that of Matz's least surprise. In any case, returning a
Fixnum is appropriate because that's the *character* representation. If
you want this, do a[1, 1] or a[1..1]. In the *future*, however (Ruby
2.0), a[1] will return a single character in the string for the
appropriate encoding of the string. There will be a different way,
however, of getting at the underlying bytes.

-austin
 
M

Martin DeMello

Austin Ziegler said:
I do not believe that this necessary.

# Equivalent to your ...LAST suggestion
"hello".index("lo") + ("lo".size - 1)

I actually think LAST (though I'd say :last) is a cleaner sulution there
- it avoids repeating the "lo", and it's definitely a useful use case.

martin
 
A

Austin Ziegler

I actually think LAST (though I'd say :last) is a cleaner sulution there
- it avoids repeating the "lo", and it's definitely a useful use case.

Well, "lo" wouldn't be what gets repeated. It'd be more like:

str.index(find) + (find.size - 1)

-austin
 
M

Martin DeMello

Austin Ziegler said:
Well, "lo" wouldn't be what gets repeated. It'd be more like:

str.index(find) + (find.size - 1)

Same thing. It's still asking the user to do something that is more
properly the computer's problem (explicitly specifying an algorithm for
a common task), and it's repeating a piece of information in the "call".

martin
 
A

Austin Ziegler

Same thing. It's still asking the user to do something that is more
properly the computer's problem (explicitly specifying an algorithm for
a common task), and it's repeating a piece of information in the "call".

Fair enough; I don't think that adding a boolean-style parameter is a
good idea, though. I'd support an #index_of_last or something like
that.

-austin
 
M

Martin DeMello

Austin Ziegler said:
Fair enough; I don't think that adding a boolean-style parameter is a
good idea, though. I'd support an #index_of_last or something like
that.

Good point - method names are a lot more self-documenting than flags.

martin
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top