How do I create a program where I use my name and outputs with a greeting?
How do I create a program where I use my name and outputs with a greeting?
puts 'Qual é seu nome?'
nome = gets.chomp
puts "E aí, #{nome}!"
Ruby is the language. Ruby on Rails is the framework for building web applications.
My recommendation is for you to study Ruby first and then go deeper in the Rails framework. This will make you suffer less on the learning curve.
Having installed Ruby on your computer, simply create a file ola.rb
(% with% is the extension of Ruby files) and start programming.
To print a text on the screen, you can use the .rb
. IO#puts
comes from the English " put string ".
puts 'Olá, meu nome é user104809!'
And this is how text is printed on the console in Ruby. To run this code, just run puts
on your console.
To receive the user name, you can use the ruby ./ola.rb
. It will be responsible for capturing an input from the user. As it returns a String with a blank line at the end ( IO#gets
- escape sequence), I'll remove it using /n
.
puts 'Qual é seu nome?'
nome = gets.chomp
puts "E aí, #{nome}!"
The result looks something like:
SeethatinthelastString#chomp
,Iuseda string interpolation ( IO#puts
). Do not panic! It's very simple. It will only insert into the string the value of an expression. That is:
"um mais um é igual a {1 + 1}."
=> um mais um é igual a 2.
It's the short way to do it:
"um mais um é igual a " + (1 + 1).to_s + "."
With the interpolation it is much better to read, right ?! Interpolations are available since Ruby 1.9 .
There are some very good sources for learning the language. If there is a possibility, let's see!