Quick tour on how Ruby on rails talks to the world
Date Published
Reading time
2 min
In this article
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-1990puts "I'm #{age} years old"# I'm 26 years old
For global, class and instance variables one can omit braces:
class Personattr_accessor :namedef hello@@times = 48puts "Hello from #@name, #@@times times in #$0"endendjoe = Person.newjoe.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 - 1990puts "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 Fixnumdef to_strself.to_sendendage = 2016 - 1990puts "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 Worldprintf "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 = <<HEREDOCI 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}HEREDOCputs large_message# I have something to say. #{3+3}
More to read

Not all AI software development companies are equal. Learn what separates firms that truly build with AI from those that just use the word. Includes real questions to ask and red flags to avoid.

SaaS made sense a decade ago. For many businesses today, custom AI-powered software delivers better ROI, faster. Here’s how to know when to make the switch, and how to do it without disrupting your operations.

Dive into Sphere's full-stack developer journey with AI – from tackling code with GitHub Copilot to unleashing problem-solving insights with ChatGPT. Explore the potential of AI in software development projects: which tools are truly handy, how many hours can you save, and what's the next big thing? Pavel Korchak shares his insights.

In 2023, interoperability is still top of the agenda for leaders and decision-makers, referring to the timely and secure access, integration and use of electronic health data to optimize health outcomes for individuals and populations.