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:
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?
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
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.