Live data from Hacker News

Ruby Koans. Best way to learn Ruby.

rubykoans.com

1–10 of 20 posts

Re: Ruby Koans. Best way to learn Ruby.

#3
Going through these would be useful for any Rubyist, not just those learning. I have been using Ruby for a while now and I didn't know there was a difference between string << 'foo' and string += 'foo' until now.

Re: Ruby Koans. Best way to learn Ruby.

#8
post #3

Going through these would be useful for any Rubyist, not just those learning. I have been using Ruby for a while now and I didn't know there was a difference between string << 'foo' and string += 'foo' until now.

I learned that difference while doing some genetic programming. The difference is quite visible when you do it a few million times.

Re: Ruby Koans. Best way to learn Ruby.

#9

  def test_changing_hashes
    hash = { :one => "uno", :two => "dos" }
    hash[:one] = "eins"

    expected = { :one => "eins", :two => "dos" }
    assert_equal expected, hash

    # Bonus Question: Why was "expected" broken out into a variable
    # rather than used as a literal?
  end
Can anyone help with the bonus question? From searching Google, I read that the reason:

    assert_equal { :one => "eins", :two => "dos" }, hash
gives an error is that Ruby believes this to be a block. But then why doesn't it consider the hash to be a block below:

    expected = { :one => "eins", :two => "dos" }
?

Edit: thanks jashkenas for the explanation! Edit2: for anybody else trying these, I think the version in github is more up-to-date, with some corrections:

http://github.com/edgecase/ruby_koans

Post reply on HN