Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created July 19, 2012 03:02
Show Gist options
  • Save kmandreza/3140484 to your computer and use it in GitHub Desktop.
Save kmandreza/3140484 to your computer and use it in GitHub Desktop.
fun_string!
Exercise 4
Create a new instance method on the String class called fun_string! which is called on a String object and permanently modifies it by capitalizing the even characters (counting from 1) and then reversing the string. See if you can do it in a single line.
For example,
string = "apples"
string.fun_string!
string # now equals "SeLpPa"
word = "apples"
split_word - word.split('')
split_word.each_with_index do |x, i|
if i.odd?
x.upcase!
end
end
reversed_word= split.word.reverse
reversed_word.join
OR
word = 'apples'
word.split('').each_with_index {|x,i| x.upcase! if i.odd?}.reverse.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment