Push an array to another array using Ruby and return square brackets
I spent several hours looking for a way to drag an array to another array or hash. Sorry in advance if the formatting of this question is a little messy. This is the first time I have asked a question on StackOverflow, so I am trying to properly care for my questions.
I need to write some code to create the following test block:
class TestNAME < Test::Unit::TestCase
def test_directions()
assert_equal(Lexicon.scan("north"), [['direction', 'north']])
result = Lexicon.scan("north south east")
assert_equal(result, [['direction', 'north'],
['direction', 'south'],
['direction', 'east']])
end
end
The simplest thing I came up with is below. The first part goes through, but then the second part doesn't return the expected result when I run rake test
.
Instead of this:
[["direction", "north"], ["direction", "south"], ["direction", "East"]]
it returns:
["north", "south", "east"]
Although, if I print the y result as a string in the console, I end up with 3 separate arrays that are not contained in another array (as shown below). Why didn't it print the outer square brackets of the array, y ?
["direction", "north"] ["direction", "south"] ["direction", "east"]
Below is the code I wrote in an attempt to pass the test block above:
class Lexicon
def initialize(stuff)
@words = stuff.split
end
def self.scan(word)
if word.include?(' ')
broken_words = word.split
broken_words.each do |word|
x = ['direction']
x.push(word)
y = []
y.push(x)
end
else
return [['direction', word]]
end
end
end
Any feedback on this would be much appreciated. Thank you all in advance.
source to share
What you see is the result each
that returns the repeating thing, or in this case broken_words
. You want collect
one that returns the converted values. Please note that your original is y
never used, it is simply thrown away after it is composed.
Here's the fixed version:
class Lexicon
def initialize(stuff)
@words = stuff.split
end
def self.scan(word)
broken_words = word.split(/\s+/)
broken_words.collect do |word|
[ 'direction', word ]
end
end
end
It's worth noting that a few things have changed here:
- Splitting into an arbitrary number of spaces, not just one.
- Simplification for one case instead of two.
- Elimination of redundant operator
return
.
One thing you might want to consider is using a data structure for example { direction: word }
. This makes referencing values ββmuch easier as you would entry[:direction]
by avoiding ambiguous ones entry[1]
.
source to share
If you are not creating Lexicon objects, you can use a module which can make it clearer that you are not creating objects.
Also, there is no need to use an extra variable (i.e. broken words), and I prefer the {} block syntax in the do..end syntax for function blocks over iterative blocks.
module Lexicon
def self.scan str
str.split.map {|word| [ 'direction', word ] }
end
end
UPDATE : Based on Cary's comment (I assume he meant split when he scanned), I removed the extra argument to split.
source to share