
Submitted by neoneye (Sun Oct 24 11:10:49 UTC 2004)
#first and #last methods, just for this purpose, to extract the sub-array either from the beginning or the ending. Why not also use #first and #last on String ?
"string"[-4, 4] #-> "ring" It feels somewhat redundant that I have to type the length 2 times. A very simple operation that easily can confuse people if they are browsing through others code.. this is a place where one easily can introduce off-by-one errors.
There are many ways to extract the ending substring, which IMHO is less clean, such as "string"[/.{4}\z/] and "string"[2..-1] .
"ruby".first(2) #=> "ru" "ruby".last(3) #=> "uby"
#first be interpreted? should it interpret as bytes or chars?
Should there be aliases #left and #right ?
class String
def first(length=nil)
length ||= 1
unless length.respond_to?(:to_int)
raise TypeError, "cannot convert #{length.class} to Integer"
end
n = length.to_int
unless n.kind_of?(Integer)
raise TypeError, "#{length.class}#to_int should return Integer"
end
raise ArgumentError, "negative string size" if n < 0
n = [n, self.size].min
self[0, n]
end
def last(length=nil)
length ||= 1
unless length.respond_to?(:to_int)
raise TypeError, "cannot convert #{length.class} to Integer"
end
n = length.to_int
unless n.kind_of?(Integer)
raise TypeError, "#{length.class}#to_int should return Integer"
end
raise ArgumentError, "negative string size" if n < 0
n = [n, self.size].min
self[-n, n]
end
end

| Comments | Current voting | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|


RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
a useful addition --gmosx
I'm voting "Neutral" (why would anyone do that?). I like and - but that's Pyth..Errr Icon syntax and it would be incompatible with Ruby's :symbols.
"string".first(n) and "string".last(n) don't get any closer to a compact form, but I share your feelings about "string"[-4, 4] requiring the length twice. I've always felt there's something "not quite there" about Ruby's slice method. --daz