Values in Ruby

Here’s the Ruby translation of the Go code example, formatted in Markdown suitable for Hugo:

Ruby has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.

# Strings, which can be concatenated with +.
puts "ruby" + "lang"

# Integers and floats.
puts "1+1 = #{1 + 1}"
puts "7.0/3.0 = #{7.0 / 3.0}"

# Booleans, with boolean operators as you'd expect.
puts true && false
puts true || false
puts !true

To run the program, save it in a file (e.g., values.rb) and use the ruby command:

$ ruby values.rb
rubylang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

In this Ruby example:

  1. We use puts to print output, which is similar to fmt.Println in the original code.
  2. String concatenation is done with the + operator, just like in the original.
  3. For integer and float operations, we use string interpolation (#{}) to embed the results directly in the output string.
  4. Boolean operations work the same way as in the original code.

Ruby, being a dynamic language, doesn’t require explicit type declarations. It automatically infers the types of values, making the code more concise compared to statically-typed languages.