![]() | ruby day 2 |
|
26 Sep 2010, 00:02
mozart reina (1 post) |
i’m having trouble implementing the tree with nested hashes, partly because i don’t really understand the structure and syntax of ruby well. can anyone provide a solution for me to reverse engineer? doesn’t need any comments, i just need to see how it’s done… i’ve spent the last two days trying to figure it out, i’ve done the other exercises. |
|
02 Oct 2010, 21:53
Finn Newick (1 post) |
I found this bit tricky too, after a little fiddling around I came up with this which is pretty simple:
This is the first bit of ruby I’ve ever written so no idea if it’s in a ‘rubonic’ (if there is such a word!) style, or what was intended in the exercise (I flipped the constructor arguments around, and my gut feeling is that that is unnecessary). Have fun |
|
13 Nov 2010, 21:24
Geert Guldentops (1 post) |
I have a slightly shorter implementation (but not necessarily better) of this problem but I am really curious what other ‘novices’ came up with:
I found the question a bit strange since it said ‘Let the initializer accept a nested structure with hashes and arrays.’ when only nested hashes are passed to the tree. Kind regards, Geert |
|
24 Nov 2010, 02:26
Horacio Gonzalez (1 post) |
I have another solution, that is more similar in spirit to the first one. Maybe it’s not the prettiest Ruby, but does the trick with only 4 lines of initialization: |
|
30 Nov 2010, 00:20
Roger Roelofs (1 post) |
I also had to think pretty hard to get this one. I’m not sure it is fluent, but it did turn out short. |
|
15 Dec 2010, 02:03
Michael Koziarski (3 posts) |
Would possibly be a little more idiomatic. |
|
21 Dec 2010, 16:51
Patrick Shields (1 post) |
Is the choice of each instead of map purposeful? Since we only care about the side effects, why would one choose one over the other? |
|
29 Dec 2010, 12:32
Vilius Normantas (1 post) |
I made an assumption that the top level hash has only one key, so I wrote it this way: |
|
15 Feb 2011, 11:55
Cristofer Weber (1 post) |
I’ve tried a recursive solution, but it appears that when at the leaf node level (maybe because of the existance of more than one element) Ruby considers as an Array, not as a Hash. Any idea?
|
|
30 Mar 2011, 21:08
Ton van Bart (1 post) |
I wanted a recursive solution as well but also wanted to guard against an input hash with more than one key at the root level.
|
|
08 Nov 2011, 20:40
Martin Mares (1 post) |
This is my version of the code. Perhaps someone will be helpful.
Output is:
|
|
04 Apr 2012, 16:16
Michael Jeppesen (1 post) |
Here’s the initialize method I came up with:
|
|
13 Jun 2012, 13:22
guanxiaohua2k6 (2 posts) |
Here is my answer. |
|
21 Oct 2012, 17:37
Pierre Sugar (7 posts) |
and another solution
|
| You must be logged in to comment |

