[#42243] コミュニティと宗教の分離について — Beyond <beyond@...>

14 messages 2006/05/21

[#42267] メニューのループについて — リックス <rubyer4649@...>

りっくすです

21 messages 2006/05/27

[#42301] Re: メニューのループについて — "conundrum /" <conundrum@...>

conundrumです。

11 messages 2006/05/28

[ruby-list:42198] Re: YAML でEUC は使えないのでしょうか。

From: Tomokiyo Nomura <tnomura@...>
Date: 2006-05-05 22:48:30 UTC
List: ruby-list #42198
On Sat, 6 May 2006 00:50:04 +0900
Tomokiyo Nomura <tnomura@mnet.ne.jp> wrote:

自己レスです。Google で検索したらちゃんと JSON モジュールがありました。

> 野村@延岡です。
> 
> YAML で EUC は使えないのでしょうか。もし使えないのなら Ruby 本体の方に
> 次のような inspect2 メソッドのようなものは実装可能でしょうか。これなら、
> JSON 程度には可読ですし、eval でオブジェクトに戻すことができますし、
> EUC や SJIS が使えます。

ロジックも変だったので次のように変更しました。車輪を2度発明してしまった...

class Object
  def to_json
    str = self.inspect
    lines = str.gsub(/\{/, "\n{ ").gsub(/\[/, "\n[ ").gsub(/\,\s*/, ",\n").to_a
    indent = 0
    lines.shift
    lines.collect! do |line|
      case line
      when /^\[/
        line.sub!(/^/, " " * indent)
        indent += 2
      when /^\{/
        line.sub!(/^/, " " * indent)
         indent += 2
      else
        line.sub!(/^/, " " * indent)
        if line =~ /([\]\}]+).*$/
          indent -= 2 * $1.size
        end
      end
      line
    end
    lines.join
  end
end

a = {"a = {"vegitables"=>["cabbage", "onion"], "apple"=>"red",
 "banana"=>"yellow"}
puts a.to_json

In This Thread