Ruby Rails Beginner

Asked

Viewed 87 times

0

How I create a program where I USE my Name and outputs with a greeting?

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

1 answer

1

TL;DR

puts 'Qual é seu nome?'
nome = gets.chomp
puts "E aí, #{nome}!"

About Ruby and Rails

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.

Playing with I/O in Ruby

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.

Where to learn Ruby?

There are some very good sources to learn the language. If possible, send!

  1. On the official website of Ruby
  2. In the interactive tutorials of Rubymonk
  3. In the Getting Started of Ruby in 15 minutes Try Ruby
  4. In the book The Ruby Way
  5. In the community study group of Training Center (I’m there!)
  • I believe this site can be of great help as well: https://gorails.com/setup/ubuntu/17.10

Browser other questions tagged

You are not signed in. Login or sign up in order to post.