top of page

[LANG] Basic Fundamentals of Ruby


This post will basically walk through all basic programming concepts for example loops/conditions using the programming language Ruby.

Ruby is a easy to learn object oriented language and in my opinion its structure is quite similar to the everyday English language that we are using so it is quite suitable for beginners as the syntax is easy to understand.

Commands to print line to the screen

Commenting

Implementing method on objects

Methods to get user input

String interpolation

Control Flow

Method that substitute a string to another string

Loop

Iterating through an array

Text segmentation

Hashmap

Iterating through hash

Defining methods

Sorting an array to ascending order

Symbol

What is a symbol?

Symbol is usually in the form :string which is normally used either as hash key or for referencing method names

They make good hashes because of the following reasons:

1. They are immutable

2. Only one copy exist

3. Symbol-as-key runs faster

Noted that when using symbols as hash keys, instead of using the symbol =>, you can simply do one:1

Converting between symbols and strings

Filtering out data from hash

Refactoring code

Ruby is an efficient language in terms of code refactoring because

you can represent a statement in a fairly simplified way - ternary conditional expression.

Case statement

Conditional assignment

Implicit return

- Return the result of last evaluated expression

Short circuit evaluation

- For example, false && true, when it sees that one of the boolean condition is false it will stops reading the rest statement when it sees && because the statement will definitely return false regardless of the value of the second boolean statement

Upto/Downto

Call and response

Collect

Yield

Proc Syntax

Why using procs?

1. Block cannot be called over and over again without writing it

2. Procs are objects

:to_i is a type of Proc (&:to_i)

You can do proc.call to call the Proc

Lambda

Actually, lambda = Proc.new.

So, what is the difference between lambda and proc?

1. Lambda will check number of argument passed to it and will throw error message but proc will just return assign nil to missing objects

2. Lambda return passes control back to the calling method, when a proc returns, it finishes immediately without going back to the calling method

What is the difference between blocks and procs?

Blocks are referring to bit of code between do ... end or { };

Proc are saved blocks.

 

Object oriented programming

Initialise method

Inheritance

Scope

Reading/Writing Attribute

What is a module?

- A toolbox that contain a set methods and constraints

Interface

Featured Review
Tag Cloud
No tags yet.
bottom of page