Menu
CoddyTech

String Operations

Lesson 6 of 48 in Coddy's Ruby for Go Developers course.

Go strings are immutable byte sequences; len counts bytes and range decodes UTF-8 runes. Ruby strings are mutable and carry an encoding, with length counting characters for valid encoded text. Ruby upcase returns another string and upcase! mutates. ASCII examples hide the distinction between bytes and characters, so choose operations deliberately for general text.

Go: complete program

package main
import ("fmt")

func main(){
word:="map"; fmt.Println(word[:2])
}

Ruby: complete program

name = "elm"
puts "<#{name.upcase}>"
puts name
challenge icon

Challenge

Easy

Strip surrounding whitespace, lowercase the input and replace each internal space with a hyphen. Internal repeated spaces each produce a hyphen. Input contains ASCII letters and spaces. Return (empty) if the result is empty.

Return text from solve(input) without printing. The locked driver prints the returned text with one final newline and checks required public APIs independently.

Your code goes in exercise.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'exercise'
def check(value)
  raise 'Public API check failed' unless value
end

puts solve(STDIN.read)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Ruby for Go Developers

Practice on your own: Online Ruby compiler