Ruby Koans: about_hashes.rb

I

Ilya B.

Question about this method in Ruby Koans -> about_hashes.rb

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

expected = { :eek:ne => "eins", :two => "dos" }
assert_equal true, expected == hash

# Bonus Question: Why was "expected" broken out into a variable
# rather than used as a literal?
end

Referring to the bonus question, is there any reason other than that
it's more readable this way? Seems to accomplish the same thing?
 
S

skim

[Note: parts of this message were removed to make it a legal post.]

That sounds about right. Readability.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

That sounds about right. Readability.

Question about this method in Ruby Koans -> about_hashes.rb

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

expected = { :eek:ne => "eins", :two => "dos" }
assert_equal true, expected == hash

# Bonus Question: Why was "expected" broken out into a variable
# rather than used as a literal?
end

Referring to the bonus question, is there any reason other than that
it's more readable this way? Seems to accomplish the same thing?

If that were true, shouldn't it be:
assert_equal expected , hash

I think it is probably a mistake, and they meant to try and show that you
can't do either of these, because it looks like you are passing a block.
assert_equal { :eek:ne => "eins", :two => "dos" } , hash
assert { :eek:ne => "eins", :two => "dos" } == hash
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,104
Latest member
LesliVqm09
Top