Skip to content

Instantly share code, notes, and snippets.

@kattak
Last active January 31, 2017 03:27
Show Gist options
  • Select an option

  • Save kattak/49432e61abe9ab7cc9cbd5464f86376d to your computer and use it in GitHub Desktop.

Select an option

Save kattak/49432e61abe9ab7cc9cbd5464f86376d to your computer and use it in GitHub Desktop.
What's the error?

#Scope in Ruby

def longest_palindrome(words)
  words.split.each do |word|
    #Initialize current longest palindrome 
    curr_palindrome = ""
    
      #replace current palindrome if longer 
      curr_palindrome = word if word.length > curr_palindrome.length && palindrome?(word) 
  end
  return curr_palindrome
end 

##Error curr_palindrome is out of scope. Only accessible within the each loop on lines 3 - 9

undefined local variable or method `curr_palindrome' for main:Object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment