Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Ruby Simple Symbols
    I am confused about why this is not an acceptable answer for the Simple Symbols problem. Can anyone help me understand why this is not right? Thanks!
    def SimpleSymbols(str)
    
      value = false  
      this_string = str.chars
      test = []
    
      this_string.each_with_index do |character, index|
        if character.match(/\w/)
              if index == 0 || index == -1
                value = false
                break
              elsif this_string[index-1] == "+" && this_string[index+1] == "+"
                value = true
              else
                value = false
                break
              end
          end
      end
      return value        
    end
    
    rafiki asked this question on 4/14/14 | ruby
    Answers
  • +
  • 1
  • -
  • As far as I see, each_with_index is a method of Enumerable Module since quite early version of Ruby. From the error message, I guess you should use double quotes around your string in the textfield of the webpage(for exampe "a+" instead of a+).
  • +
  • 0
  • -
  • FWIW, my code is different than yours, but I'm also employing /\w/. Works great on my computer, but getting error in the CB challenge. Guess I will go have a try with [a-zAz] instead. Good point in previous post that jthoupt offered about \w including numbers and _. Pretty sure that violates the parameters of the challenge. Cheers.
  • +
  • 0
  • -
  • Thanks for the ideas. That doesn't seem to be the problem (although [a-zA-Z] is totally better, you're right). I actually get the correct answers for all the test cases when I run them manually on my computer; it looks like the coderbyte system doesn't like the each_with_index method for some reason, since I get this error message: (eval):7: undefined local variable or method `a' for main:Object (NameError) from (eval):43:in `each_with_index' from (eval):6:in `chars' from (eval):6:in `each_with_index' from (eval):6:in `SimpleSymbols' from (eval):24 Is it possible that this is a Ruby version mismatch? I have Ruby 2.0.
  • +
  • 0
  • -
  • 1. reuglar exp /\w/ will match more than [a-zA-Z]. You can use [a-zA-Z] or /[[:alpha:]]/ instead. 2. index would not be negative, you can print out the index to check this. (though this may not affect the correctness)
  • +
  • 0
  • -
  • \w/ includes 0-9 and '_'. Have you tried [a-zA-Z] instead?
    Login to write an answer