Learn Ruby on Rails: Learn Ruby on Rails Series, Part 3

In this tutorial, the material that will be covered is an introduction to the Ruby language. Before we continue with the hardware, let’s remember what ruby ​​is?

Daftar Isi

A. Ruby Overview

Ruby is a purely object-oriented programming language developed by Yukihiro Matsumoto (Matz) in 1993. Ruby is an interpreted language, which means that to run Ruby you do not need to compile it, but simply run it in the interpreter. Ruby is also a dynamic language. We don’t need to declare variables, but just do the assignment directly.

In Ruby, everything is an object, which means Each piece of information and code can be assigned properties and actions. Soon ruby, no primitive types. Everything is an objectincluding integers, strings, arrays, classes, etc.

B. Using Ruby

After installation, how do I run Ruby? Isn’t it useless if you install it but don’t use it? A simple way to run Ruby can be done via irb (interactive ruby)

IRB East tool which works to execute Ruby commands. To use tool This can be done by running a command in the terminal or command prompt as follows.

$ irb 
$ puts "Hello Guys!"

C. Ruby Class

Class is the “plane” or “blueprint” of the object. Classes are used only to create a basic framework. What we will use later is the printed result of the course, namely object.

Do classwe use keywords class
followed by name class. Class names in Ruby must start with an uppercase letter. Usually, Ruby courses are written in a way camelize. For example like this

class Person

def hint

END

def show_name

END

END

D. Ruby Method

In Ruby, everything is an object. So whether it’s in the form of a string or a number, it’s an object. An object will usually have multiple methods that can be called and used

To call a method, we use the dot (.) syntax followed by the method name. For example, if we want to know the amount byte from a string, we can use the method byte size.

“to welcome”.byte size

It’s important to remember that when writing methods in Ruby, you need to use keywords defSO nama methodthen ends with END. An example is as follows

Next, in the tutorial, we will cover several operations on strings and numbers.

E. Types of strings and examples of their use

String is a Ruby object whose function is to manipulate a series of bytes, which generally represent the letters of a sentence. Ruby has a built-in class called String which has several methods that we often use to program Ruby.

Generally, creating or declaring a string is done by surrounding the string with signs double quote (“) Or unique quote (‘).

To display strings, you can usually use the syntax puts. For example, as follows.

puts “Let’s learn the ruby”

The way to insert a variable into a string is to use #{}

name = “Learn Ruby on Rails”

puts “We are #{name}


String method

Here are some commonly used string methods

  • try_convert(obj)
    To convert object to a chain. Returns the converted string or nothing If object cannot be converted.

    String.try_convert("str") #=> "str"

  • str * int
    Returns a new string of integers

    "Ho" * 3 #=> "Ho" "Ho" "Ho"

  • str << obj
    Combine obj has str
    without produce object new

    "Hello from" << "Indonesia" #=> "Hello from Indonesia"

  • str + vol
    Combine obj has str
    And produce object new

    "Hello from" + "Indonesia" #=> "Hello from Indonesia"

  • str.capitalize
    change the first letter to uppercase, creating a new object.
  • str.capitalize!
    mengubah huruf pertama menjadi huruf kapital dengan mengubah object yang ada.

  • str.upcase
    change all letters to uppercase, creating a new object.

  • str.upcase!
    change all letters to uppercase by editing the existing object.

  • chain length
    returns the length of the string.




F. Digital

Numeric is the base class for numbers in Ruby. Because in Ruby everything is object, if you define 1, then 1 is the object of fixed

Example:

puts 100.class # Fixnum

puts (100.2).class # Float

From the example there is a word fixed, float.

  • Fixed used for integers
  • Float used for decimals

G. Boolean

Ruby has boolean values true And falsewhich is an object of class TrueClass And FalseClass. In rubies, all values ​​apart false And nil is considered true and can be used in Boolean operations, such as conditions, and, or and so on.

jester = TRUE

puts “current boolean value #{jester}


H. Conditional declaration

In programming languages, of course, there is an important part, namely conditional statements. This conditional statement is a command used to check whether the expression being executed is correct or not.

I. Presentation of the case

Case statements in Ruby have keywords case And When. This case statement is not much different from to change And case in other programming languages. The difference between the case and the moment is that this case can check the similarity or equivalence of all types of objects that can be compared.

case shoes

When “flat shoes”

puts “use flat shoes”

When “sneakers”

puts “use sneakers”

When “corners”

puts “use wedges”

When “heels”

puts “use heels”

END


Case can also be used in comparison, for example as follows

case age

When age < 11

puts “children”

When 11..17

puts “teenager”

When 18..25

puts “mature”

other

“out of reach”

END


J. Ternary Operator

This operator is a short form of if else. Examples are as follows.

score = 85

predicate = score > 75 ? “GOOD” : “Bad”

K. Loops

Looping or repetition is an important thing which is widely used in creating applications in any programming language. In Ruby, loop is written with a structure like this.

An example is the following.

until I >= 5

puts “It’s a ruby ​​buckle.”

I += 1

END

L. Iteration and block

Iteration is the same as loops such as until and while, but in the application creation process itself, many developers make blocks combined with iterators.

(1..5).each TO DO |num|

puts number

END

3.times TO DO

print “Ho! »

END

Mr. Array vs Hash

Arrays are one of Ruby’s built-in classes. An array is a collection of objects ordered and accessible based on their index. In Ruby, arrays can store any objects such as strings, fixnums, hashes and even other arrays. Each element of the array is associated and accessible with a certain index. Automatically arrange table elements indexed with numbers starting at 0.

While Hash is a collection of pairs key-value. Hashes are similar to arrays, but if arrays are indexed with integers starting at 0, hashes are indexed with Ruby objects. All objects in Ruby can be used as keys or values ​​in a hash. But in general, we use symbols or strings as a hash key more often.

painting = []

chop = {}

painting = [‘a’,‘b’,‘c’,‘d’]

puts painting[0]

puts painting[1]

chop = {:A => ‘A’, “two” => ‘b’}

puts chop[:one]

puts chop[“two”]

Fathara Annisa Azka | Hello Indonesia

PakarPBN

A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.

In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.

The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.

Jasa Backlink

Download Anime Batch