At 2009-07-20 02:20PM, "Joel VanderWerf" wrote: > > What's the best way to write this program so that it will run correctly > on both 1.8 and 1.9? This works, but I'm just curious if there's > anything better, preferably without the RUBY_VERSION test, without > adding methods to String, and without losing much efficiency compared > with 1.8's String#[]. > > if RUBY_VERSION =~ /\A1\.9/ > def third_char_as_fixnum(s) > s[2].ord > end > else > def third_char_as_fixnum(s) > s[2] > end > end > > s = "abc" > p third_char_as_fixnum(s) # ==> 99 This works in both 1.9.1 and 1.8.7 def third_char_as_fixnum(s) s[2].ord end Because Integer#ord returns the integer receiver. -- Glenn Jackman Write a wise saying and your name will live forever. -- Anonymous