Ruby is one of the leading programming languages in the world. It is the open source programming language which is easily modified, distributed and copied. The programming using this language is difficult if you don’t know the proper syntax of the language so according to the language structure you must recall a fair share of syntax, commands, and functions. Ruby is the pure object-oriented and flexible programming language because it allows a user to alter parts freely. Hire freelancers who have knowledge and experience in Ruby programming language.

Ruby Console Basics:

The console is the effective way of interacting with the computer. The console is also known as command line, command prompt, and terminal. Through this console, you can do anything with your graphical desktop user interface. Following are some list of the commands.

Cd- Change directory to the specified directory on the console.

Ls- List all content of a directory.

pwd- Shows the current directory

mkdir- Create a new directory

rm- Delete the directory.

Variables in Ruby:

Variables are the memory location which holds the data used in a program. There are five types of variables in Ruby programming language.

  • Global Variables: Global Variables begins with the $.
  • Instance Variables: Instance variables begins with @
  • Class variables: Class variables begin with the @@
  • Local Variables: Local variables begins with lower case letter
  • Constant: Constant variables begin with upper case letter.

Operators in Ruby:

Ruby programming language has an extensive set of operators. Following are some types of operators.

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Parallel Assignment Operators
  • Bitwise Operators
  • Logical Operators
  • Ternary Operators
  • Range Operators

Conditional Statements in Ruby:

Conditional statements enable your program to make correct decisions and perform right actions. Following are the list of the conditional operators.

  • if else statement

Syntax

if conditional [then]

code…

[elsif conditional [then]

code…]…

[else

code…]

end

 

  • Unless Statement

Syntax

unless conditional [then]

code

[else

code ]

end

 

  • Case Statements

Syntax

case expression

[when expression [, expression …] [then]

code ]…

[else

code ]

end

 

  • While Statements

Syntax

while conditional [do]

code

end

 

  • Until Statements

Syntax

until conditional [do]

code

end

 

  • For Statement

Syntax

for variable [, variable …] in expression [do]

code

end

Regular Expression:

A regular expression is a sequence of characters which allows you to match or find other strings or set of strings using a specialized syntax held in pattern between slashes or between arbitrary delimiters followed by %r. Hire freelancers who have knowledge and experience in Ruby programming language.

Syntax

/pattern/

/pattern/im    # option can be specified

%r!/usr/local! # general delimited regular expression

Strings in Ruby:

The Ruby holds string objects and manipulates an arbitrary sequence of one or more bytes typically representing characters which represent human language. The simplest string literals get enclosed in single quotes, and the text within the single quotes is a string.

Syntax

‘This is a simple Ruby string literal.’

Array in Ruby:

The array is the collection of a similar type of objects and its stores the fixed sized sequential elements. The arrays of Ruby have an ordered way; integer-indexed collections of any object and each element in an array are associated with and referred to by an index. The array holds an object such as String, Integer, Fixnum, Hash, and other array objects. You can find multiple freelance services to work on Ruby

Syntax:

There are many ways to create or initialize an array. Following are the syntax of the creation of the array.

Names = Array.new

Exception Handling in Ruby:

The exception and execution always happen together. If you are trying to open a file which does not exist and if you do not handle this situation properly, then your program fails to run.

If any exception occurs, the program stops working. The exception is the process of handling different errors that occur during a program execution and performs a perfect action instead of stopping the program execution. The programming language Ruby has an excellent mechanism for handling exceptions. Following are the syntax of exception.

Syntax

begin

# –

rescue OneTypeOfException

# –

rescue AnotherTypeOfException

# –

else

# Other exceptions

ensure

# Always will be executed

end

Multithreading:

More than one thread of execution is known as multithreading. In each thread, statements execute sequentially, but different threads get executed parallelly. Ruby allows the programmer to write multithreaded programs with the thread class. The thread concept in Ruby is lightweight and achieves efficiency in concurrency of code.

Syntax

# Thread #1 is running here

Thread.new {

# Thread #2 runs this code

}

# Thread #1 runs this code

Summary:

This article is about the Ruby programming cheat sheet. This cheat sheet covers all the important syntax of the Ruby language, and it is most useful when you are performing the programming in Ruby programming language.