Ruby 1.9/Rails method return anomality?

Hello

I have a different return value from a method when the last line is
either

str.encode ‘UTF-8’
return str.encode ‘UTF-8’ # or:
str.encode! ‘UTF-8’

In the first case the string is not in UTF-8! Both other return a
correct string. I changed this several times back and forth and tried it
again because I could not believe it. It really behaves like this.

This strange problem occurs with the following routine:

def convert2utf8 str, charset
str.force_encoding charset
rescue
str.force_encoding(‘BINARY’).gsub!(Regexp.new(’[\x00-\x08\x0B\x0C\x0E-\x1F0\x80-\xFF]’,nil,‘n’),
‘.’)
ensure
str.force_encoding(‘BINARY’).gsub!(Regexp.new(’[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\xFF]’,nil,‘n’),
‘.’) unless str.valid_encoding?
str.encode ‘UTF-8’
end

When ‘str’ (comes from an email parsed with Mail 2.2.15 so str.encoding
returns #Encoding:US-ASCII) consists of ISO-8859-1 encoded string and
‘charset’ is ‘ISO-8859-1’ this happens in a Rails controller. I cannot
reproduce this in plain irb (will try in Rails console tomorrow). So is
this a Rails problem? Kinda ‘lazy encode’ problem or something?

Regards, T.

On Feb 9, 8:20pm, “T. N. T.” [email protected] wrote:

correct string. I changed this several times back and forth and tried it
str.force_encoding(‘BINARY’).gsub!(Regexp.new(’[\x00-\x08\x0B\x0C\x0E-\x1F\
x80-\xFF]’,nil,‘n’),
‘.’) unless str.valid_encoding?
str.encode ‘UTF-8’
end

When ‘str’ (comes from an email parsed with Mail 2.2.15 so str.encoding
returns #Encoding:US-ASCII) consists of ISO-8859-1 encoded string and
‘charset’ is ‘ISO-8859-1’ this happens in a Rails controller. I cannot
reproduce this in plain irb (will try in Rails console tomorrow). So is
this a Rails problem? Kinda ‘lazy encode’ problem or something?

The return value of a method isn’t the result of the ensure block -
it’s whatever happens before (ie either str.force_encoding charset or
your rescue clause).
The latter two alternatives work, either because the change what is
being returned to be what you want, or because they modify the string
in place

For example, if
def foo
1
ensure
2
end

then foo() evaluates to 1, not 2

Fred

Frederick C. wrote in post #980654:

The return value of a method isn’t the result of the ensure block -
it’s whatever happens before (ie either str.force_encoding charset or
your rescue clause).
The latter two alternatives work, either because the change what is
being returned to be what you want, or because they modify the string
in place

For example, if
def foo
1
ensure
2
end

Oh, so simple! I didn’t test it with an ensure clause. So I feel the
floor under my feet is still solid. Thank you!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs