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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 9, 8:20 pm, "T. N. T." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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? >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> Regards, T. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung 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 > endOh, so simple! I didn''t test it with an ensure clause. So I feel the floor under my feet is still solid. Thank you! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.