From: Joey Zhou Date: 2011-04-24T21:45:06+09:00 Subject: Re: Does String#encode in Ruby 1.9.2 have option :fallback? Y. NOBUOKA wrote in post #994724: > Hi, > > You can use :fallback option on ruby 1.9.2. > I think you must not set :undef option when you use :fallback option. > > # example on ruby 1.9.2p180 > # U+3042 is a Japanese character, and it cant express in US-ASCII > "\u3042".encode( Encoding::US_ASCII, fallback: { "\u3042" => 'a' } ) > #=> "a" Thank you! It works. Is there any tricky skill about the :fallback? str = "\u4ced\u9d12" #replace_hash = {"\u4ced"=>"x"} replace_hash = Hash.new {|hash,key| "[#{key.ord}]"} print str.encode("gbk", fallback: replace_hash) I want to do this, but failed. It seems :fallback cannot be a dynamic hash? -- Posted via http://www.ruby-forum.com/.