Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 372 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the answer to the bonus question in test_changing_hashes of Ruby Koans?

#1
In the [Ruby Koans][1], the section about_hashes.rb includes the following code and comment:

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

expected = { :one => "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

I can't figure out the answer to the bonus question in the comment - I tried actually doing the substitution they suggest, and the result is the same. All I can figure out is that it is for readability, but I don't see general programming advice like that called out elsewhere in this tutorial.

(I know this sounds like something that would already be answered somewhere, but I can't dig up anything authoritative.)


[1]:

[To see links please register here]

Reply

#2
I compare the variable expected with the hash, not broke

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

assert_equal expected, hash
```

Reply

#3
It's because you can't use something like this:

assert_equal { :one => "eins", :two => "dos" }, hash
Ruby thinks that `{ ... }` is a block, so it should be "broken out into a variable", but you can always use `assert_equal({ :one => "eins", :two => "dos" }, hash)`
Reply

#4
Another test you can use is the following:

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

I’ve simply swapped the parameters to `assert_equal`.
In this case Ruby will not throw an exception.

But it still seems a bad solution to me. It’s much more readable using a separate variable and testing a boolean condition.
Reply

#5
I thought it was more readable, but you can still do something like this:

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through