<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>galo</title>
    <link>https://ruby-china.org/galo</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>请教，用 Vim 怎么 debug ruby 程序啊？</title>
      <description>&lt;p&gt;我现在只能在怀疑有错的地方，用 p 把变量打印出来。有没有更方便的方法，能一步一步调试的？&lt;/p&gt;</description>
      <author>galo</author>
      <pubDate>Thu, 02 Apr 2015 09:38:20 +0800</pubDate>
      <link>https://ruby-china.org/topics/24955</link>
      <guid>https://ruby-china.org/topics/24955</guid>
    </item>
    <item>
      <title>又有问题了，还是关于 Hash 的</title>
      <description>&lt;p&gt;代码如下
&lt;code&gt;hsh1 = {a:[1, 2, 3, 4], b:[2, 4, 6, 8]}&lt;/code&gt;
&lt;code&gt;hsh2 = hsh1.dup&lt;/code&gt;
&lt;code&gt;hsh2[:a] &amp;lt;&amp;lt; 5&lt;/code&gt;
本来预期只有 hsh2 的第一个 value 多了 5，但实际上 hsh1 和 hsh2 都被修改成了
&lt;code&gt;{a:[1, 2, 3, 4, 5], b:[2, 4, 6, 8]}&lt;/code&gt;
看起来 hsh2 虽然是 dup 的 hsh1，但是两者的 value 还是有关联&lt;/p&gt;

&lt;p&gt;但是当 Hash 表的 value 是字符串时，例如
&lt;code&gt;hsh1 = {a:"hello ", b:"hey "}&lt;/code&gt;
&lt;code&gt;hsh2 = hsh1.dup&lt;/code&gt;
&lt;code&gt;hsh2[:a] += "ruby"&lt;/code&gt;
此时只有 hsh2 变成了{a:"hello ruby", b:"hey "}
hsh1 还是{a:"hello ", b:"hey "}
看起来 value 是字符串时，dup 时把字符串是完全复制的，hsh1 和 hsh2 的 value 不再有关联&lt;/p&gt;

&lt;p&gt;我想知道，当 Hash 表的 value 是什么时，dup 是完全复制的，和原件不再有关系。请大神们指点。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;更新：&lt;/strong&gt;
我已经意识到自己之前理解错了，实际上 Hash 在 dup 的时候，只是 dup 了 Hash 表本身，每个 value 都没有 dup。hsh1 和 hsh2 的 value 都是一样的，上边的例子之所以给人以错觉，是因为 hsh2[:a] += "ruby"这一行已经改变了 hsh2[:a] 的 object id，所以 hsh2[:a] 的变化没有影响到 hsh1[:a]。
看来 ruby 的 Hash 表在 dup 时，只 copy 了第一层。要想 deep copy，一楼 chenge 提供的方法可以完美的解决这个问题，谢谢。&lt;/p&gt;</description>
      <author>galo</author>
      <pubDate>Fri, 06 Feb 2015 16:15:14 +0800</pubDate>
      <link>https://ruby-china.org/topics/24146</link>
      <guid>https://ruby-china.org/topics/24146</guid>
    </item>
    <item>
      <title>请教一个关于 Hash 的问题</title>
      <description>&lt;p&gt;代码如下：
&lt;code&gt;hsh = Hash.new([])&lt;/code&gt;
&lt;code&gt;hsh["a"] &amp;lt;&amp;lt; 1&lt;/code&gt;
&lt;code&gt;hsh["b"] &amp;lt;&amp;lt; 2&lt;/code&gt;
&lt;code&gt;p hsh&lt;/code&gt;
我想得到的是
&lt;code&gt;hsh = {"a"=&amp;gt;[1], "b"=&amp;gt;[2]}&lt;/code&gt;
但是实际上 hsh 还是空的
更奇怪的是，这时候 hsh["a"] 和 hsh["b"] 的值都是 [1, 2]
似乎此时 hsh 的默认值变成了 [1,2]
百思不得其解，请大神指点。&lt;/p&gt;</description>
      <author>galo</author>
      <pubDate>Thu, 05 Feb 2015 10:52:15 +0800</pubDate>
      <link>https://ruby-china.org/topics/24130</link>
      <guid>https://ruby-china.org/topics/24130</guid>
    </item>
    <item>
      <title>请教一个关于 Proc 的问题</title>
      <description>&lt;p&gt;各位大大，我最近初学 ruby，发现一个奇怪的问题。我看书上讲可以将 Proc 对象前面加上&amp;amp;，作为 block 传递给方法。
&lt;code&gt;pr1 = Proc.new {puts "hello"}&lt;/code&gt;
&lt;code&gt;5.times &amp;amp;pr1&lt;/code&gt;
这两行可以成功运行，输出五行 hello
但是，我试了 Proc 对象的 lambda 定义方法
&lt;code&gt;pr2 = lambda {puts "hello"}&lt;/code&gt;
&lt;code&gt;5.times &amp;amp;pr2&lt;/code&gt;
程序报错了：“ArgumentError: wrong number of arguments (1 for 0)”，说参数不对。
我想了很久没明白，请教各位大大是怎么回事，谢谢。&lt;/p&gt;</description>
      <author>galo</author>
      <pubDate>Fri, 12 Dec 2014 15:03:52 +0800</pubDate>
      <link>https://ruby-china.org/topics/23156</link>
      <guid>https://ruby-china.org/topics/23156</guid>
    </item>
  </channel>
</rss>
