Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Ruby on this site has strange behavior?
    Hello! Is it just me or when running a lot of ruby code on this website, the code doesn't behave the same way as on my IRB or if I execute the code locally? This is the third time it happens, but I am currently working on Letter Changes - my code which works fine on my machine when executed on this site, returns a NIL. Is my code just wrong or what is happening??
    def LetterChanges(str)
      offset =1 
      i = 0
      result = ""
      while i < str.length
        if str[i].ord > 96 && str[i].ord < 123
         loc = str[i].ord 
          loc += 1
          loc = loc.chr
          result += loc
        else
          result += str[i]
        end
        
        i += 1
      end
      
      return result
             
    end
    
    Deterius posted this question on 11/24/15 | ruby
    Answers
  • +
  • 2
  • -
  • Unless you just copied the code part, you left out the LetterChanges(STDIN.gets) call. No value is being passed unless that call is in there.
  • +
  • 1
  • -
  • I agree with you all. I just ran some code for the LetterCapitalize problem and my IRB shows the below code as correct, but wrong in CoderByte.
    def LetterCapitalize(str)
    
      str = str.split(' ').map { |w| w.chars.map {|c| c == w[0] ? c.capitalize! : c}.join}.join(' ')
      return str 
             
    end
       
  • +
  • 0
  • -
  • I'm having the same problem with many of the Ruby challenges. For example, here's my code for Swap Case, which works fine on my machine, but not here.
    def SwapCase(str)
    
      # 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
    
      # code goes here
      a = str.chars
      (0..a.length).each do |i|
        if a[i] =~ /[A-Z]/
          a[i].downcase!
        elsif a[i] =~ /[a-z]/
          a[i].upcase!
        end
      end
      return a.join
             
    end
       
    # keep this function call here 
    # to see how to enter arguments in Ruby scroll down   
    SwapCase(STDIN.gets)    
    
    Some folks over at Stack Overflow suggest that Coderbyte is using an older version of Ruby. I've contacted the site to ask about this, but have yet to hear back.
  • +
  • 0
  • -
  • I haven't tried Ruby yet but there are some flaws in the PHP execution as well. This site was primarily for js, so I guess that's the script language that should be used.
    Log in to write an answer.