[#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:42196] YAML でEUC は使えないのでしょうか。

From: Tomokiyo Nomura <tnomura@...>
Date: 2006-05-05 15:50:04 UTC
List: ruby-list #42196
野村@延岡です。

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

class Object
  def inspect2
    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
      when /[\}\]]/
        line.sub!(/^/, " " * indent)
        indent -= 2
      else
        line.sub!(/^/, " " * indent)
      end
      line
    end
    puts lines
    lines.to_s
  end
end

a = {"vegitables"=>["cabbage", "onion"], "apple"=>"red", "banana"=>"yellow"}
b = a.inspect2
p b

<実行結果>

{ "vegitables"=>
  [ "cabbage",
    "onion"],
  "apple"=>"red",
  "banana"=>"yellow"}
"{ \"vegitables\"=>\n  [ \"cabbage\",\n    \"onion\"],\n  \"apple\"=>\"red\",
\n  \"banana\"=>\"yellow\"}"

In This Thread

Prev Next