Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3428 > unrolled thread
| Started by | Joey Zhou <yimutang@gmail.com> |
|---|---|
| First post | 2011-04-24 05:49 -0500 |
| Last post | 2011-04-24 09:40 -0500 |
| Articles | 5 — 2 participants |
Back to article view | Back to comp.lang.ruby
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
| From | Joey Zhou <yimutang@gmail.com> |
|---|---|
| Date | 2011-04-24 05:49 -0500 |
| Subject | Does String#encode in Ruby 1.9.2 have option :fallback? |
| Message-ID | <815cbeada89786414de358800a28765e@ruby-forum.com> |
My Ruby version is: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
I find in the installer's help file that String#encode has an option
:fallback.
:fallback Sets the replacement string by the hash for undefined
character. Its key is a such undefined character encoded in source
encoding of current transcoder. Its value can be any encoding until it
can be converted into the destination encoding of the transcoder.
ruby-doc.org has the same explanation:
http://www.ruby-doc.org/core/classes/String.src/M001113.html
However I can't make it effective in my code:
replace_hash = {"\u4ced"=>"x","\u4cd2"=>"y"}
ARGF.each_line do |line|
puts line.encode("gbk", :undef => :replace, :fallback => replace_hash)
end
and I found in this article
(http://www.artima.com/forums/flat.jsp?forum=123&thread=313197), which
said :fallback is new feature in Ruby 1.9.3
I am confused. Can I use :fallback option in latest Ruby 1.9.2p180?
--
Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | "Y. NOBUOKA" <nobuoka@r-definition.com> |
|---|---|
| Date | 2011-04-24 06:30 -0500 |
| Message-ID | <BANLkTi=K1Haw2L0VjXOhaHvUWkf0A2jZQg@mail.gmail.com> |
| In reply to | #3428 |
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"
--
NOBUOKA Yu
[toc] | [prev] | [next] | [standalone]
| From | Joey Zhou <yimutang@gmail.com> |
|---|---|
| Date | 2011-04-24 07:45 -0500 |
| Message-ID | <71f4ddaf98a0130c4733d1e8411bf7cc@ruby-forum.com> |
| In reply to | #3429 |
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/.
[toc] | [prev] | [next] | [standalone]
| From | "Y. NOBUOKA" <nobuoka@r-definition.com> |
|---|---|
| Date | 2011-04-24 08:28 -0500 |
| Message-ID | <BANLkTinwMaDB9aSn7YogZXm4vc3XeSB2bw@mail.gmail.com> |
| In reply to | #3430 |
> I want to do this, but failed. It seems :fallback cannot be a dynamic
> hash?
No, it can't on ruby 1.9.2p180...
However, this feature has been requested and was accepted [1],
so you can use this feature on ruby 1.9.3dev!
[1] http://redmine.ruby-lang.org/issues/4125 (Japanese)
$ ruby -v
ruby 1.9.3dev (2011-04-24 trunk 31329) [x86_64-linux]
$ irb
ruby-head :001 > h = Hash.new{ 'a' }
=> {}
ruby-head :002 > "\u3042".encode Encoding::ASCII, fallback: h
=> "a"
--
NOBUOKA Yu
[toc] | [prev] | [next] | [standalone]
| From | Joey Zhou <yimutang@gmail.com> |
|---|---|
| Date | 2011-04-24 09:40 -0500 |
| Message-ID | <edeb421ac46f1f3110ff19d519e857c2@ruby-forum.com> |
| In reply to | #3433 |
Y. NOBUOKA wrote in post #994739: > No, it can't on ruby 1.9.2p180... > However, this feature has been requested and was accepted [1], Quite a good feature! Thank you! Joey -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web