Ruby Source Code Examples

Welcome to our comprehensive blog dedicated to all things Ruby! Whether you're just starting out on your programming journey or are looking to refine your existing Ruby knowledge, this page is your one-stop destination.

Ruby Basics 

Ruby - Print Hello World
Ruby - Get User Input
Ruby - Display Current Date and Time
Ruby - Print Environment Variables
Ruby - Explain Variable Scopes
Ruby - Calculate the Sum of Two Numbers
Ruby - Find the Factorial of a Number
Ruby - Check If a Number is Even or Odd
Ruby - Generate a Random Number
Ruby - Implement the Pythagorean Theorem
Ruby - Calculate the Square Root of a Number
Ruby - Check if a Number is Prime
Ruby - Find the Greatest Common Divisor (GCD)
Ruby - Check Leap Year
Ruby - Print Power of a Number
Ruby - Print Fibonacci Series
Ruby - Count the Number of Digits in a Number

Ruby Operators

Ruby - Unary operators
Ruby - Arithmetic operators
Ruby - Bitwise operators
Ruby - Logical operators
Ruby - Ternary operator
Ruby - Assignment operators
Ruby - Comparison operators

Ruby String

Ruby - Convert a String to Uppercase and Lowercase Ruby - Reverse a String
Ruby - Count Vowels in a String
Ruby - Find the Length of a String
Ruby - Replace Substrings
Ruby - Split a String into Words
Ruby - Join an Array into a String
Ruby - Check if a String Starts with Another String
Ruby - Check if a String is a Palindrome

Ruby Array

Ruby - Find the Length of an Array
Ruby - Find the Maximum Element in an Array
Ruby - Find the Minimum Element in an Array
Ruby - Search for an Element in an Array
Ruby - Sort an Array
Ruby - Reverse an Array
Ruby - Array Create, Add, and Remove Example
Ruby - Merge Two Arrays

Ruby Hash

Ruby - Create and Use a Hash
Ruby - Find the Size of a Hash
Ruby - Check if a Key Exists in a Hash
Ruby - Merge Two Hashes
Ruby - Iterate Over a Hash

Ruby Control Statements

Ruby - If-Else Condition
Ruby - Case Statement
Ruby - While Loop
Ruby - For Loop

Ruby OOPs Concepts

Ruby - Define and Instantiate a Simple Class
Ruby - Instance Variables
Ruby - Class Methods and Variables
Ruby - Inheritance Example
Ruby - Polymorphism Example
Ruby - Abstraction Example
Ruby - Modules and Mixins
Ruby - Encapsulation Example
Ruby - Association Example
Ruby - Composition Example
Ruby - Aggregation Example
Ruby - method_missing Example
Ruby - define_method Example
Ruby - Create Custom Exception Class

Ruby File Handling

Ruby - Create a New File and Write Content
Ruby - Open and Read from a File
Ruby - Append Data to an Existing File
Ruby - Delete a File
Ruby - Different File Modes
Ruby - Read a File Line by Line
Ruby - Create a New Directory
Ruby - Rename a Directory
Ruby - Delete a Directory
Ruby - Check if a File Exists
Ruby - Get a File Size
Ruby - Check if a Path is a File or a Directory
Ruby - Get File Modification Time
Ruby - Count Number of Words in a File
Ruby - Count Occurrences of a Specific Word in a File
Ruby - Compress a File using Gzip
Ruby - Extract a Gzip Compressed File
Ruby - Create a Tar Archive

Ruby Exceptions

Ruby - Handle Division by Zero Exception
Ruby - Handle NoMethodError by Using begin-rescue-end

Ruby String Conversions

Ruby Convert String to Hash
Ruby Convert String to JSON
Ruby Convert String to Integer
Ruby Convert String to Array
Ruby Convert String to Date
Ruby Convert String to CamelCase
Ruby Convert String to Boolean
Ruby Convert String to Float
Ruby Convert String to DateTime