From: Austin Ziegler Date: 2005-10-01T08:40:44+09:00 Subject: Re: suggested changes for ruby libraries On 9/30/05, jdm wrote: > 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 -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca