Created
July 19, 2012 03:02
-
-
Save kmandreza/3140484 to your computer and use it in GitHub Desktop.
fun_string!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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