J
Joel VanderWerf
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
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