Menu
CoddyTech

Symbols and Keys

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

Go typed constants and iota can define named numeric values, but they do not create Ruby symbols or automatically map every value to text. Ruby symbols are distinct from strings and commonly serve as hash keys. A symbol and a string with matching spelling can coexist as separate keys. Establish a consistent key representation at API boundaries.

Go: complete program

package main
import ("fmt")
type Mode int
const(Slow Mode=iota;Fast)

func main(){
mode:=Fast;fmt.Printf("%d:%t\n",mode,mode==Fast)
}

Ruby: complete program

data = {mode: 1, "mode" => 2}
puts data.length
puts data[:mode]
challenge icon

Challenge

Easy

Read one command: start, stop or any other text. Look up its symbol in a fixed table: start maps to 1, stop maps to 0. Unknown commands return -1.

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