At Mon, 25 Nov 2002 21:43:28 +0900, > > [ruby-dev:18887] String#substr? > > Takaaki Tateishi proposed a method for checking if a sub-string is exactly > > same as other sub-string without creating a new string object. The method > > suppresses the excessive cost of creating new strings. > > Matz approved the idea, however rejected its method name 'substr?'. > > What's the difference between this and String#include? ? I proposed a method like str1.include?(str2,beg,n), which means that str1[beg,n] is equal to str2[0,n]. If we write "str1[beg,n] == str2[0,n]", we extract sub-strings and new objects are created. I think this brings much cost when we want to repeat to compare two sub-strings whereas we don't need new sub-strings. Here is a sample implementation in ruby. class String def substr?(str, offset=nil, len=nil) offset ||= 0 len ||= str.length for i in 0..(len-1) if( self[i+offset] != str[i] ) return false end end return true end end -- Takaaki Tateishi <ttate / kt.jaist.ac.jp>