Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Simple Symbols Problem (Ruby)
    So I am getting failing results through coderbyte but when I run on my own machine this code works. Thoughts would be really great to see why this isn't working on this site:
    def SimpleSymbols(str)
      str.downcase!
      return false if ('a'..'z').include?(str[0]) || ('a'..'z').include?(str[-1])
      str.each_char.with_index do |letter, index|
        if index > 0 && index < str.length - 1
          if ('a'..'z').include?(letter)
            unless (str[index - 1] == '+') && (str[index+1] == '+')
              return false
            end
          end
        end
      end
      return true
    end
    
    BillKozby83 asked this question on 10/25/15 | ruby, symbol
    Answers
  • +
  • 0
  • -
  • hi-You most likely have moved way past this, but this would do the trick: # Note: if using array indexing to retrieve a character from a string # for example: if str = "hello" and you do str[0] then you get back # the ASCII value for the character instead of the actual character # so to correct this simply do: str[0].chr
  • +
  • 0
  • -
  • for some reason, in this environment, str[index] returns the ascii decimal instead of the character so str[index] == '+' would evaluate as false because its sayins 43 == '+' instead of '+' == '+'. That may be why it differs from the website and your machine
    Log in to write an answer.