Ruby question on splitting and indices

Was hoping someone could help explain the the logic behind the code below. I understand most it of other than when concatenating part[0] into initials. We split the string into an array, in this case (“Kelvin Bridges”) into [“Kelvin”, “Bridges”]. Isn’t “Bridges” technically at an index of 1? Or does |part| evaluate “Kelvin” and "Bridges as separate arrays?

def to_initials(name)
      parts = name.split(" ")
      initials = ""
      parts.each { |part| initials += part[0] }
      return initials
end

puts to_initials("Kelvin Bridges")      # => "KB"

parts is ["Kelvin", "Bridges"]

So that’s a list with two items.

So for each item of parts, concatenate the first character (the character at index 0) to initials

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.