Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3430

Re: Does String#encode in Ruby 1.9.2 have option :fallback?

From Joey Zhou <yimutang@gmail.com>
Newsgroups comp.lang.ruby
Subject Re: Does String#encode in Ruby 1.9.2 have option :fallback?
Date 2011-04-24 07:45 -0500
Organization Service de news de lacave.net
Message-ID <71f4ddaf98a0130c4733d1e8411bf7cc@ruby-forum.com> (permalink)
References <815cbeada89786414de358800a28765e@ruby-forum.com> <BANLkTi=K1Haw2L0VjXOhaHvUWkf0A2jZQg@mail.gmail.com>

Show all headers | View raw


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/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Does String#encode in Ruby 1.9.2 have option :fallback? Joey Zhou <yimutang@gmail.com> - 2011-04-24 05:49 -0500
  Re: Does String#encode in Ruby 1.9.2 have option :fallback? "Y. NOBUOKA" <nobuoka@r-definition.com> - 2011-04-24 06:30 -0500
    Re: Does String#encode in Ruby 1.9.2 have option :fallback? Joey Zhou <yimutang@gmail.com> - 2011-04-24 07:45 -0500
      Re: Does String#encode in Ruby 1.9.2 have option :fallback? "Y. NOBUOKA" <nobuoka@r-definition.com> - 2011-04-24 08:28 -0500
        Re: Does String#encode in Ruby 1.9.2 have option :fallback? Joey Zhou <yimutang@gmail.com> - 2011-04-24 09:40 -0500

csiph-web