
- Ruby - Home
- Ruby - Overview
- Ruby - Environment Setup
- Ruby - Syntax
- Ruby - Classes and Objects
- Ruby - Variables
- Ruby - Operators
- Ruby - Comments
- Ruby - IF...ELSE
- Ruby - Loops
- Ruby - Methods
- Ruby - Blocks
- Ruby - Modules
- Ruby - Strings
- Ruby - Arrays
- Ruby - Hashes
- Ruby - Date & Time
- Ruby - Ranges
- Ruby - Iterators
- Ruby - File I/O
- Ruby - Exceptions
- Ruby - Object Oriented
- Ruby - Regular Expressions
- Ruby - Database Access
- Ruby - Web Applications
- Ruby - Sending Email
- Ruby - Socket Programming
- Ruby - Ruby/XML, XSLT
- Ruby - Web Services
- Ruby - Tk Guide
- Ruby - Ruby/LDAP Tutorial
- Ruby - Multithreading
- Ruby - Built-in Functions
- Ruby - Predefined Variables
- Ruby - Predefined Constants
- Ruby - Associated Tools
- Ruby Useful Resources
- Ruby - Quick Guide
- Ruby - Cheatsheet
- Ruby - Useful Resources
- Ruby - Discussion
- Ruby - Ruby on Rails Tutorial
Ruby Cheatsheet
The Ruby Cheatsheet provides the fundamentals of Ruby programming. It helps students and developers to build the projects and prepare for the interviews. Go through the cheat sheet and learn the concepts. Thus, this improves the coding skills.
- Basic Syntax
- Variables
- Operators
- Comments
- String
- String Interpolation
- If-else Statement
- Classes and Objects
- Break Statements
- Ruby Blocks
- Modules
- Array
- Hashes
- Ranges
- Regular Expression
- Exception Handling
- Commonly Used Library
1. Basic Syntax
This is the basic syntax of the Ruby programming language that displays the text using puts and print.
puts "Hello, World!" print "Tutorialspoint!"
2. Variables
Variables are used for the memory location. Ruby supports various types of variable −
# Defining the variables x = 10 # integer y = 20.5 # float puts x + y
3. Operators
The operators are the symbol that tells the compiler to perform logical tasks.
Operators | Description | Example |
---|---|---|
Arithmetic Operators | This is basic mathematical operations. | 'a + b', 'a - b', 'a * b', 'a / b', 'a % b' |
Relational Operators | This compares two values. | 'a == b', 'a != b', 'a > b', 'a < b', 'a >= b', 'a <= b' |
Logical Operators | This combine the conditional statements. | 'a && b', 'a || b', '!a' |
Bitwise Operators | This perform in the bit level. | 'a & b', 'a | b', 'a ^ b', '~a', 'a << b', 'a >> b' |
Assignment Operators | This assign the values to the variables. | 'a = b', 'a += b', 'a -= b', 'a *= b', 'a /= b', 'a %= b' |
Below are the some examples of operators in Ruby programming language −
# Arithmetic Operators # Addition puts 8 + 18 # Subtraction puts 9 - 3 # Multiplication puts 7 * 6 # Division puts 20 / 4 # Comparison Operators # Greater than puts 10 > 5 # Equal to puts 10 == 5 # Not equal to puts 10 != 5
4. Comments
Comments are used to show the information. The single-line comment is denoted by "#" whereas multi-line comments are written using the =begin and =end keywords.
# This is a single-line comment =begin multi-line comment =end
5. String
The strings are used to print the text. In Ruby, strings are represented using both single and double quotes.
# Strings str1 = "Hello" str2 = 'World!' puts str1 + " " + str2
6. String Interpolation
The string interpolation is the process of inserting the values with the help of expression inside the curly braces #{}.
name = "Tutorialspoint!" puts "Welcome to #{name}!"
7. If-else Statement
The if-else statement is a part of the control structure used to execute the logic conditionally, based on whether the condition is true or false.
age = 18 if age >= 18 print "You are eligible for vote." else print "You are are not eligible for vote." end
8. Classes and Objects
Classes and objects are fundamental concepts of object-oriented programming. A class defines the blueprint for an object, whereas an object is an instance of a class.
class Person def initialize(name, id) @name = name @id = id end def fun puts "My name is #{@name} and I am #{@id} years old." end end person = Person.new("Sanjay", 25) person.fun
9. break Statements
In Ruby, the break statement terminates the program loop.
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num.each do |num| if num == 7 puts "The loop break at the number #{num}" break end puts "The processing number is #{num}" end puts "Loop ended."
10. Ruby Blocks
The blocks represent the anonymous function in Ruby that is passed into the methods. The block can be declared using a single-line or multi-line block.
times { puts "Hello, Block!" }
11. Modules
In Ruby, a module is a collection of methods and constants that can be used to organize and structure code.
# example of modules module MathHelpers def square(x) x * x end end include MathHelpers puts square(4)
12. Array
In Ruby, an array defines the ordered, indexed collection of objects. The object holds the data types such as String, Integer, Fixnum, Hash, Symbol, and even other Array objects.
arr = [10, 20, 30, 40, 50] arr.each { |i| puts i }
13. Hashes
The hashes are similar to dictionaries by defining key−value pairs.
hash = { name: "Ruby", id: 3323 } puts hash[:name]
14. Ranges
The ranges are data types that show a sequence of values.
(1..5).each { |i| puts i }
15. Regular Expression
Regular Expression is defined by the special sequence of characters that help users to find the set of string matches or particular syntax held in the program.
/pattern/ /pattern/im # option can be specified %r!/usr/local! # general delimited regular expression
16. Exception Handling
In Ruby, an exception handling is the process of handling the error raised in the program. These errors occur during the execution of the program. In simple, unexpected, or unwanted events.
begin raise # block where exception raise rescue # block where exception rescue end
17. Commonly Used Library
Here, we are providing the example of the JSON library.
require 'json' data = { name: "Ruby", type: "Programming Language" } puts JSON.generate(data)