How blocks differ from Methods - string[x].method question


Hey guys. In the lesson 10 hint it explains that you can apply a string method to any element within a string by identifying it with [x]. For ex:

def capitalize(string)
puts "#{string[0].upcase}#{string[1..-1]}"

This should upcase the first letter in the string(0) and print the rest(1...). But it doesn't when I actually write this up in my text editor. I tried a simpler version as well which didn't work:

x = "this is my string"
puts "#{x[0].upcase}"



Try it with

def capitalize1(string1) 
 puts string1.chars.first.upcase + string1[1..-1]
 puts string1[1..-1]
 puts string1[0..0].upcase
 puts string1.capitalize



Thanks Leonhard, those all worked. Maybe it's because I'm using an outdated version of ruby 1.8.7. I did figure out something that works. The parameter for the first method just had to be set to 0.

puts string1[0..0].upcase + x[1..-1]

I noted the method you stated as well. :slight_smile: