Suggested changes for ruby libraries

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"

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 =~ /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="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

···

On 9/30/05, jdm <xyz@xyz.com> wrote:
--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

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

···

Austin Ziegler <halostatue@gmail.com> wrote:

I do not believe that this necessary.

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

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

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

-austin

···

On 10/1/05, Martin DeMello <martindemello@yahoo.com> wrote:

Austin Ziegler <halostatue@gmail.com> wrote:
> 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.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

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

···

Austin Ziegler <halostatue@gmail.com> wrote:

On 10/1/05, Martin DeMello <martindemello@yahoo.com> wrote:
> Austin Ziegler <halostatue@gmail.com> wrote:
> > 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.

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

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

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

···

On 10/1/05, Martin DeMello <martindemello@yahoo.com> wrote:

Austin Ziegler <halostatue@gmail.com> wrote:
> 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".

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Wouldn't this be too small a function to include into the stdlib? Why
not simply include

class String
  def index_of_last(needle)
    self.index(needle) + (needle.size - 1)
  end
end

in your program or your personal lib. Or maybe ask for inclusion in nano.
But I admit its difficult to draw the line here. What is your opinion?

best regards,

Brian

···

On 02/10/05, Austin Ziegler <halostatue@gmail.com> wrote:

On 10/1/05, Martin DeMello <martindemello@yahoo.com> wrote:
> Austin Ziegler <halostatue@gmail.com> wrote:
> > 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".

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

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

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

martin

···

Austin Ziegler <halostatue@gmail.com> wrote:

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.