3

I'm trying to make a code that returns all the even values in a string. The code I created seems to do that, but it won't return the white spaces and so the final test fails. Can someone help me to understand why it returns all the letters, but none of the whitespaces?

# Every Other Letter Define a method, #every_other_letter(string), 
# that accepts a string as an argument. This method should return a 
# new string that contains every other letter of the original string, 
# starting with the first character. Treat white-space and punctuation 
# the same as letters.

def every_other_letter(string)
  idx = 0
  final = []

  while idx < string.length 
    letters = string[idx].split
    final = final + letters
    idx = idx + 2
  end

  p final = final.join
end

puts "------Every Other Letter------"
puts every_other_letter("abcde") == "ace"
puts every_other_letter("i heart ruby") == "ihatrb"
puts every_other_letter("an apple a day...") == "a pl  a.."

and this returns:

------Every Other Letter------
"ace"
true
"ihatrb"
true
"apla.."
false
=> nil
Carl Edwards
  • 13,826
  • 11
  • 57
  • 119
  • 1
    Why do you split `string[idx]`? This gets you single char string, there's nothing to split. And yes, this is the error. – Sergio Tulentsev Nov 29 '17 at 20:28
  • There are no "even" values in a string. I believe you mean "return all characters in a string whose indices are even"? Now, what do you mean by "return"? If `str = 'abcde', do you men to return the string `'ace'`, the array `['a', 'c', 'e']` or something else. Be precise! – Cary Swoveland Nov 29 '17 at 21:22
  • Thanks Sergio. I used split because I'm new to ruby and was trying a few different things and the code worked when I tried split, except for including the spaces. Thanks to your comment I see if I just change split to split('') it will pass all 3 tests. Of course it's still unnecessary and I should clean up my code which I'll do instead. For reference without split, the exact code posted produces this error message: no implicit conversion of String into Array (repl):13:in `every_other_letter' (repl):21:in `
    '
    – Mike Barnes Nov 29 '17 at 21:28
  • @CarySwoveland the instructions are in the code, but basically yes it would turn "abcde" into "ace". – Mike Barnes Nov 29 '17 at 21:29
  • `'hello'.chars.each_slice(2).map(&:first).join #=> "hlo"` is another way. – Sagar Pandya Nov 30 '17 at 04:46

2 Answers2

4

Just another way to get all even-index characters, using a regular expression to grab pairs and replacing each pair by just its first character:

"abcd fghi".gsub(/(.)./, '\1')
=> "ac gi"

Or find them and join them:

"abcd fghi".scan(/(.).?/).join
=> "ac gi"
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
2

The problem is, as pointed @Sergio, that you're using split over a single char, so, you're "converting" that letter in an array. What you can do is just to push the string[idx] to final and it works for you that way.

In other way you could split the string, using select get the chars where the index is even, and then join them:

p "an apple a day...".chars.select.with_index { |_, i| i.even?  }.join == "a pl  a.." # true
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59