Skip to content

Instantly share code, notes, and snippets.

@RickArora
Created November 20, 2018 04:48
Show Gist options
  • Save RickArora/37ae6a0352ad532fd37e29faf6769dec to your computer and use it in GitHub Desktop.
Save RickArora/37ae6a0352ad532fd37e29faf6769dec to your computer and use it in GitHub Desktop.
# Return the middle character of a string. Return the middle two characters if
# the word is of even length, e.g. middle_substring("middle") => "dd",
# middle_substring("mid") => "i"
def middle_substring(str)
finalString = ""
if (str.length % 2) == 0
x = str.chars
finalString += x[(x.(length/2).ceil]
finalString += x[x.(length/2).floor]
else
x = str.chars
finalString += x[x.(length/2).ceil]
end
return finalString
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment