LoginSignup
3
3

More than 5 years have passed since last update.

C#でRubyっぽいデバッグ表示(Dictionary編)

Last updated at Posted at 2013-02-16

前回からの続き。

Dictionary (Rubyでいう所のHash)

Rubyだとこう

hash.rb
dict = {aaa:1, bbb:2, ccc:3}
p dict

C#だとこんな感じになる

Dict.cs
using System;
using System.Collections.Generic;
using System.Linq;

var dict = new Dictionary<string, int>() {
    {"aaa", 1},
    {"bbb", 2},
    {"ccc", 3},
};
Console.WriteLine("{" + String.Join(", ", from v in dict select v.Key + "=>" + v.Value) + "}");

シンボルが文字列になっている以外はほぼ同じ。

{:aaa=>1, :bbb=>2, :ccc=>3}    (Ruby)
{aaa=>1, bbb=>2, ccc=>3}       (C#)

おまけ: Rubyでpinspectを使わずに自力で書く場合

jiriki.rb
puts "{" + dict.map{|key,value| "#{key}=>#{value}"}.join(", ") + "}"```

C#でRubyっぽいデバッグ表示一覧

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3