From: Daniel DeLorme Date: 2007-08-12T22:27:55+09:00 Subject: Re: first and last char Stefan Rusterholz wrote: > Daniel DeLorme wrote: >> def first; self[/\A./m]; end >> def last; self[/.\z/m]; end >> >> >> $KCODE='u' >> => "u" >> >> "日本語".first >> => "日" >> >> "日本語".last >> => "語" >> >> Daniel > > You are right, your solution is better. Only partly. Unfortunately, end-anchored regular expressions have pretty abysmal performance. >> require "benchmark" >> str = "日本語"*1000 >> Benchmark.measure{10000.times{str.first}}.real => 0.0704410076141357 >> Benchmark.measure{10000.times{str.last}}.real => 5.35788202285767 :-( Daniel