Some programming languages are hard to learn and some are easy. Ruby is one of the later. Although Ruby has simple syntax it is very powerful programming language. What’s more, it is also quite versatile. You can use it to for web projects as well as for desktop applications. Give a Ruby a chance. Learn what you need to become a Ruby programmer. This article will help you with it.

Getting Started With Ruby the Easy Way Part 1 (Comments, Variables, Strings).

Getting Started With Ruby the Easy Way Part 3 (Data Types Pt2, Control Flow Pt1).

Getting Started With Ruby the Easy Way Part 4 (Control Flow Pt2).

Getting Started With Ruby the Easy Way Part 5 (Control Flow Pt3).

Getting Started With Ruby the Easy Way Part 6 (Methods).

Getting Started With Ruby the Easy Way Part 7 (Recursion, Scope, OOP Pt1).

Getting Started With Ruby the Easy Way Part 8 (OOP Pt2).

Getting Started With Ruby the Easy Way Part 9 (OOP Pt3).

Getting Started With Ruby the Easy Way Part 10 (OOP Pt4 and Beyond).

Getting User Input

Every programming language should provide some way to get input from the user. This also applies to Ruby. When you want to get user’s input in Ruby, all you need is to use the gets method. This method will then return what the user types as a string. If you want to store the input you got for later, you can do it by assigning the return value of the gets method to a variable.

As you learned, the default value you will get when you use gets method is a string. What if the input you are asking for is a number and you need to get it in this data type? Fortunately, Ruby offers a quick and easy way to solve this issue. The solution is to use gets.to_i method. This will again get the input as a string, but Ruby will immediately automatically convert it to an integer.

There is one more thing you need to about the gets method. When you use it, you will get a line of text. And, this includes the new line at the end. This is a good thing if you ask for input multiple times and want to have each result on separate line. However, there might be situations when this will not be desirable. Then, in order to remove the new line, use the gets.chomp method.

##
## Example of "gets" method
## and assigning the return value to a variable
x = gets

## Print the value to console
puts x

##
## Example of the "issue" with new line
## Print instructions to console
puts "Please enter your name:"

## Ask for user's input
name = gets

## Print the result to console, using string interpolation
puts "Welcome, #{name}."

## Output when the input is "Tommy" (notice the dot ending the sentence on a new line):
## Please enter your name:
## Welcome, Tommy
## .

##
## Example of solving "issue" with new line using "gets.chomp"
## Print instructions to console
puts "Please enter your name:"

## Ask for user's input
name = gets.chomp

## Print the result to console, using string interpolation
puts "Welcome, #{name}."

## Output when the input is "Tommy" (notice the dot right after the name):
## Please enter your name:
## Welcome, Tommy.

Data Types Pt1

Now, it is time for one of the most important topic in both, Ruby and also programming in general. Data types. What are data types? These are types of “things” in your code that are used to represent data. These data include numbers, text and other values. IT is basically what you, as a Ruby programmer, will work with the majority of time.

In Ruby, there are many less or more exotic data types, eight primary and 3 additional. Those eight primary Ruby data types are those you will probably use the most often. So, let’s focus on those. What data types are we talking about? These primary data types are constants, integers, floats, strings (text), booleans, symbols, arrays and hashes.

Before you learn about all individual types, there is one thing you need to know about. You don’t need to remember what data is which type when you work with it. Every time you assign some value to variable Ruby will do the heavy lifting. Meaning, Ruby will automatically determine and set appropriate data type for you. Now, let’s talk about individual data types.

#ruby #programming #design development

Getting Started With Ruby the Easy Way Pt2
1.50 GEEK