Sphere Partners

Quick tour on how Ruby on rails talks to the world

Date Published

Reading time

2 min
Quick tour on how  Ruby on rails talks to the world
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

:

ruby
puts "Hello World"
# Hello World

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

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

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

ruby
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:

ruby
puts "Hello " + "World"
# Hello World

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

ruby
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:

ruby
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.

ruby
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):

ruby
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:

ruby
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:

ruby
"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:

ruby
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:

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

More to read

How to Choose an AI Software Development Company (And What to Watch Out For) — hero image
Consulting & Advisory,  Tech Executive Advisory,  Data & AI,  IT Strategy Consulting,  Software Development,  ChatGPT,  Trends

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.

Exploring the Integration of AI in Software Development: A Full-Stack Developer's Perspective
Software Development,  Data & AI,  ChatGPT

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.

We'd love to hear from you!

Please provide your contact details, and our team will get back to you promptly.