Quick tour on how Ruby on rails talks to the world

05 Apr 2016

A quick reminder about producing text messages in Ruby.

Talk to the world

The basic method for text output is puts from IO:

puts "Hello World"
# Hello World

With interpolation it’s possible to insert some expression in a string:

age = 2016-1990
puts "I'm #{age} years old"

# I'm 26 years old

For global, class and instance variables one can omit braces:

class Person
  attr_accessor :name

  def hello
    @@times = 48
    puts "Hello from #@name, #@@times times in #$0"
  end
end

joe = Person.new
joe.name = "Joe"
joe.hello
# Hello from Joe, 48 times in irb

Strings can be concatenated via + or concat method:

puts "Hello " + "World"

# Hello World

But we can’t do the same with other types:

age = 2016 - 1990
puts "I'm " + age + " years old"

# TypeError: no implicit conversion of Fixnum into String

String’s + uses implicit type conversion using argument’s to_str method. So we can do the following patching and use numbers in concatenation:

class Fixnum
  def to_str
    self.to_s
  end
end

age = 2016 - 1990
puts "I'm " + age + " years old"

# I'm 26 years old

So if one wants to use a custom class in similar expressions, to_str method needs to be added to the class.

Structured Talks

In a number of methods ruby follows some formatting conventions. printf prints the string to the output stream using format string, sprintf (aliased as format) puts the result of formatting in a returned string. string#% uses the same format string.

The format sequence used there allows developer to control numbers appearence (precision, numeral system) and alignment. It has the following syntax:

%[flags][width][.precision]type

type defines the general output behavior for an argument (string, number or #inspect), flags regulate the options and width~/~precision set the corresponding appearns options. for the detailed description parameters see Kernel#sprintf documentation. Format string content which is not a format sequence is put as is.

printf "Hello World"
# Hello World
printf "Year %d", 2016
# Year 2016

Let’s print some aligned numbers. %10.2f stands for 10 position output, 2 digits after dot, right aligned (default):

printf "Expences:\nJan/02:%10.2f;\nJan/04:%10.2f;", 1234.50, 354.10
# Expences:
# Jan/02:   1234.50;
# Jan/04:    354.10;

Do the same but left aligned (- flag) and treat numbers as integers:

printf "Expences:\nJan/02:%-10d;\nJan/04:%-10d;", 1234.50, 354.10
# Expences:
# Jan/02:1234      ;
# Jan/04:354       ;

String#% method uses the same format sequence. Applying more than one argument to a format string requires an Array. Hash can be used for named parameters:

"Jan/02:%010d" % 1234
# Jan/02:0000001234
">%7s: %8.2f" % ["Jan/8", 3425.2399]
# >  Jan/8:  3425.24
"Hello %{name}" % { name: 'World' }
# Hello World

Delivering large messages

Large text also known as heredoc can be created the following way. Any identifier (instead of HEREDOC below) might be used:

large_message = <<HEREDOC
I have something to say.
...
But that is quite a long story.
HEREDOC

Interpolation is allowed, but if you want to disable it put the heredoc identifier in a single quote:

large_message = <<'HEREDOC'
I have something to say. #{3+3}
HEREDOC

puts large_message
# I have something to say. #{3+3}