Coder Social home page Coder Social logo

getting-user-data-command-line-codealong's Introduction

Getting User Data in the Command Line

Overview

Up to this point, we've been able to create simple Ruby programs by using the puts method to output a string to the command line. In this codealong, we'll use the gets method to get data from the user and assign it to a variable for future use.

Objectives

  1. Use the gets method to get data from the user.
  2. Assign the return value of gets to a variable
  3. Understand that gets always returns a string.
  4. Show that gets has a trailing space, so we usually use .chomp to remove it.

Video

<iframe width="560" height="315" src="https://www.youtube.com/embed/ZED78EO-3Hc" frameborder="0" allowfullscreen></iframe>

Getting Data from the User in the Command Line

Using gets

We're going to create a simple program called greeter.rb, that takes in a name from a user, and puts a welcome phrase based on their input. Start by creating a new ruby file called greeter.rb and prompt the user for their name:

puts "What is your name?"

Run the program to ensure that your program works.

In order to create a prompt in Ruby, use the gets method, like so:

puts "What is your name?"
gets

Now when you run your program, it will pause until you fill in the prompt and hit the return key. Your program should then end.

We now have a prompt, but the value that the user has given us isn't being saved for us to use. Why don't we use a variable to hold the data that our user is giving us? That way we can use the data further on in the program:

puts "What is your name?"
name = gets
puts "Welcome to the Flatiron School, " + name + "!"

Run your program again. You'll now be able to input your name, and will get back a welcome phrase that looks something like this:

Welcome to the Flatiron School, Danny
!

Trailing Newlines

Do you notice anything weird about the output of the string? The "!" is on the next line! The reason we see this is that when we use the gets method to input our name, we have to hit return to move on to the next line of the program. This return is actually counted as part of the string we're saving to the name variable. This is called a "trailing newline", and has an easy fix. We use the .chomp method to remove trailing newline characters:

puts "What is your name?"
name = gets.chomp
puts "Welcome to the Flatiron School, " + name + "!"

Run your program again - it should work!

Gets with Numbers

Note: gets will always return a string, regardless of whether the user is inputting numbers or letters in the prompt. As such, if you're planning on using the input as an integer or a float, you'll need to convert the input to the correct data type, using .to_i for integers and .to_f for floats. In fact, gets is actually short for 'get string'! Check out this "Addition program" that uses .to_i to convert the user input to integers for addition:

puts "Welcome to the addition machine. Please input your first number:"
number1 = gets.to_i
puts "Input your second number:"
number2 = gets.to_i
sum = number1 + number2
puts "The sum of the two numbers is " + sum

We're very close, but this will give us the error TypeError: String can't be coerced into Fixnum - that's because we're trying to add together a string and an integer in our last line. We need to convert the integer back to a string using .to_s (to string) in order to use concatenation:

puts "Welcome to the addition machine. Please input your first number:"
number1 = gets.to_i
puts "Input your second number:"
number2 = gets.to_i
sum = number1 + number2
puts "The sum of the two numbers is " + sum.to_s

Resources

RubyLearning: Getting Input

View Getting User Data in the Command Line on Learn.co and start learning to code for free.

getting-user-data-command-line-codealong's People

Contributors

deniznida avatar dfenjves avatar mhackettnyc avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

getting-user-data-command-line-codealong's Issues

String Interpolation

In the previous lesson, "String Interpolation" it's suggested that + variable_name + is a method for interpolation we should avoid and instead we should use #{variable_name}. Yet, here in this lesson you revert to + variable_name + in the first coding example. I understand that it's a valid method and works, but shouldn't the lessons be consistent?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.