Reputation: 43
Why is my code not printing the joined array? With this code the array is printing the modified elements but not joined...
def translate(string)
vowels=['a','e','o','u','i']
string=string.split
string.map! do
|x| if vowels.include? x[0]
x.insert(-1,'ay')
else
x=x.slice(1, x.length)
x=x.insert(-1, x[0]+'ay')
end
end
string.join('-')
print(string)
end
Upvotes: 1
Views: 67
Reputation: 451
The Array#join
method doesn't set itself as the joined string, instead it just returns a joined string. You can save the joined result into string
itself like this:
# ...
string = string.join('-')
print string
Of course, use another variable can be more readable:
# ...
joined_string = string.join('-')
print joined_string
Upvotes: 3
Reputation: 16506
This is because you are not storing the value returned by join
anywhere. string
itself is still an Array. Try this instead:
print string.join('-')
Thus your method should look like:
def translate(string)
vowels=['a','e','o','u','i']
string=string.split
string.map! do
|x| if vowels.include? x[0]
x.insert(-1,'ay')
else
x=x.slice(1, x.length)
x=x.insert(-1, x[0]+'ay')
end
end
print string.join('-')
end
Upvotes: 1
Reputation: 405765
You're not printing the value returned from the join
.
joined = string.join('-')
print(joined)
Upvotes: 1