0
How I create a program where I USE my Name and outputs with a greeting?
0
How I create a program where I USE my Name and outputs with a greeting?
1
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 that you study the Ruby language first and then go deeper into the Rails framework. This will make you suffer less in the learning curve.
Having installed Ruby on your computer, just create a file ola.rb
(.rb
is the Ruby file extension) and start programming.
To print a text on the screen, you can use the method IO#puts
. puts
comes from the English "put string".
puts 'Olá, meu nome é user104809!'
And that’s how you print a text on the console in Ruby. To run this code, just run ruby ./ola.rb
on your console.
To receive the user name, you can use the method IO#gets
. It will be responsible for capturing a user input. How it returns a String with a blank line at the end (/n
- escape sequence), I will remove using the String#chomp
.
puts 'Qual é seu nome?'
nome = gets.chomp
puts "E aí, #{nome}!"
The result is something like:
See that at last IO#puts
, I used a string interpolation (#{nome}
). Don’t be scared! It’s very simple. It will only insert the value of an expression into the string. 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 interpolation is much better to read, right?! Interpolations are available from the Ruby 1.9.
There are some very good sources to learn the language. If possible, send!
I believe this site can be of great help as well: https://gorails.com/setup/ubuntu/17.10
Browser other questions tagged ruby-on-rails ruby
You are not signed in. Login or sign up in order to post.
ruby is a programming language, ruby on Rails is a framework made using the ruby language. Your question doesn’t make it clear where you have the problem.
– Alex Takitani