Ruby Tutorial: part one, learn Ruby with examples and diagrams

in #utopian-io5 years ago (edited)

521ghjg.png

Repository

https://github.com/ruby/ruby

Introduction

Hello dear steemians, today I want to make you a tutorial a little different, oriented to learn programming from scratch using diagrams and Ruby language. In this tutorial we will be doing programming examples where we will explain the logic of the program through Data Diagrams, a powerful tool used by programmers and then we will write them in Ruby programming language and explain them step by step.

I will not explain how to install it since there is a lot of information in many places on how to install the Ruby language, and our purpose with this tutorial is to teach you how to program using examples of all kinds and develop the learning of the Ruby programming language by looking at its use and behavior in practical cases.

I invite you to enjoy this tutorial that I'm sure you will love.

In it you will learn:

  • You will learn the use of Ruby programming language with the use of examples explained to try to make it simpler and more fun.

This is a series of tutorials where later we will install libraries and use frameworks related to ruby and interact with the steem API, as we go forward in the tutorial.

Requirements

You must have basic programming skills to be able to install Ruby and understand some of the technical languages used in the tutorial.

Difficulty

Provided you have some programming experience this tutorial is basic level.

Content of the tutorial:

First case:

In all programs I consider that the first thing we should learn is how to write messages on screen, with creating input and output data in our program so this is the first thing we will do.

We will show it with a very simple program, the sum of two numbers. Our diagram would look like this:

hhfhhr.png

puts "enter the first number"
number1 = gets.to_i
puts "enter the following number"
number2 = gets.to_i
sum = number1 + number2
puts " the result of the sum is: "
puts sum

It must always be considered that in the programming language Ruby the capital letters are differentiated from the small letters, therefore number, house, thing, each one is totally different to Number, House, Thing and so on, the same would apply for the reserved words of the language.

So it is not the same to write.

puts " enter the first number "

Instead of:

PUTS " enter the first number "

This would give a syntax error.

Well stemians let's go to the explanation:

Let's show a message on the screen telling the operator to load the first value called number1, using the puts instruction:

puts " enter the first number "

For keyboard data entry we use the gets method which is an instruction that allows you to enter any character of the keyboard.

Then we place a point in the method gets and define the type of data we want to enter, in this case, we call the method to_i to convert the data entered to a whole data type.

number1 = gets.to_i

The variable number1 stores the integer value entered by keyboard.

In the same way we do it with the second number:

puts " enter the first number” 
number1 = gets._i

The variable "sum" stores the sum of the contents of the variables number1 and number2.

Next we must show a message per screen:print "the result of the sum is" and the content of the sum variable is also shown by calling the puts method, taking into account that the name of the variable is not enclosed in quotation marks:

puts sum

We can see the results on screen, and friends already made our first program in ruby, to run we must save it with a name and extension.rb and run it in the ruby console window writing ruby filename.rb and this will run, ready you are already programming in ruby.

Concepts that must be clear:

For the moment we will do all the algorithm inside a file with extension *.rb.

Most frequently used reserved words in Ruby

These are some of the most used reserved words in Ruby, I recommend you to learn them or have them at hand, this is fundamental in our way to absolutely master the Ruby programming language.

class      Defines a class; closes with end.
Def        Starts defining a method; closes with end.
defined?    Determines whether a method, variable, or block exists.
do Begins a block; closes with end.
Else      Executes the code that continues if the precondition is not true. Works with if,
elsif,    unless or case.
And      Logical operator, equal to && but with less precedence.
end      Ends a block of code.
False    Logical or Boolean false.
True     Logical or Boolean true.
for         Begins a loop for. Use with in.
if           Executes a block of code if the conditional statement is true. Closes with end.
in          Used with loop for.
when     Starts a clause under.
while     Executes a block of code while the conditional statement is true.
yield      Executes a block passed to a method.
FILE_     Name of the contemporary source file.
LINE_     Number of the contemporary line in the contemporary source file.
rescue    Evaluates an expression after an exception is raised. Used after ensure.
retry       When called out of rescue, repeats a method call. Inside rescue jumps to a higher block.
return     Returns a value from a method or a block.
then       Separator used with if, unless, when, case, and rescue.
Unless   Executes a block of code if the conditional declaration is false.
until       Executes a block of code while the conditional statement is false.

Currículum

This is the first tutorial of a series oriented to the learning of the Ruby programming language using examples and diagrams.

Proof of work performed

https://github.com/difelice5000/Tutorial-Ruby-

fdjfg.png

Sort:  

Thank you for your contribution @difelice5000.
After an analysis of your tutorial we suggest the following points listed below:

  • There are already many tutorials on how to learn the Ruby language on the internet as in the Steemit platform. We suggest you to explain something more innovative about this language so that the open source community learn something new.

  • Using the first person in the tutorials makes it difficult to understand the tutorials. We suggest using the third person in your text.

  • In your tutorials put more images. A tutorial with lots of text and code gets a little tiring to read.

Thank you for your work in developing this tutorial.
Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your advice.

Thank you for your review, @portugalcoin! Keep up the good work!

Hi @difelice5000!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @difelice5000!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.30
TRX 0.11
JST 0.033
BTC 63968.82
ETH 3136.80
USDT 1.00
SBD 4.28