What does string[1..-1] do?


#1



what does string[1..-1] do?


# method that capitalizes a word
def capitalize(string) 
  puts "#{string[0].upcase}#{string[1..-1]}"
end

capitalize("ryan") # prints "Ryan"
capitalize("jane") # prints "Jane"

# block that capitalizes each string in the array
["ryan", "jane"].each {|string| puts "#{string[0].upcase}#{string[1..-1]}"} # prints "Ryan", then "Jane"

Replace this line with your code.

`


#2

@jkm345,

Have a look at some documentation

http://www.rubyist.net/~slagell/ruby/strings.html
http://stackoverflow.com/questions/3611586/ruby-string-slice-index-strn-infinity
https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation


#3

It goes from the second element to the last element of the array.
Ex.

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
puts "#{a[1..-1]}"    #=> [2, 3, 4, 5, 6, 7, 8, 9, 10]

#4

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
puts "#{a[1..-1]}"

will output,

2345678910

since it only outputs the values, not the array.

In the case of a string,

b = 'everyday hero'
puts "#{b[1..-2]}"

will output,

veryday her