CodeChef Logo CodeChef Logo
Courses

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Programming and DSA

Explore courses

Catalogue

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Explore courses
Practice Compete Compiler
Upgrade to Pro
Courses

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Programming and DSA

Explore courses

Catalogue

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Explore courses
Practice Compete Compiler
Home » Wiki » Basic introduction to Ruby

Basic introduction to Ruby

Ruby can be a very efficient and powerful language for various scripting tasks. It is a more object oriented version of Python. While it lacks the richness of libraries which Python has, some might say that it is a more elegant language. Ruby also offers a fair bit of support for constructs often used in functional programming. Simple Ruby tasks, so that you may get a glimpse of how elegant the syntax is.

Computing the cartesian product of sets

Cartesian product of [B, C, F] x [G, H, I] = [ [B,G], [B, H], [B, I], [C, G], [C, H], [C, I]]

irb(main):001:0> [1,2,3].product [4,5,6] => [1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6]

Computing the Difference and Symmetric Difference of Two Sets

B - A = elements in B but not in A
A - B = elements in A but not in B
Symmetric difference = (B - A) union (A - B)


irb(main):002:0> a = [1, 4, 6]
=> [1, 4, 6]
irb(main):003:0> b = [2, 4, 6, 7]
=> [2, 4, 6, 7]
irb(main):004:0> a - b
=> [1]
irb(main):005:0> (a - b) + (b - a)
=> [1, 2, 7]

Computing the GCD using the Euclidean Method (recursive)

GCD (x, y) = GCD (x%y, y) if both (x > 0 and y > 0)
= x if y == 0
= y if x == 0


def gcd(x,y)
if x % y == 0
y
else
gcd(y, x % y)
end
end
p gcd(4, 16)
#=> 4

Checking if a number is Prime

There is a "Prime" package in Ruby.
This may be used to verify whether a number is prime or not

irb(main):008:0> require 'prime'
=> true
irb(main):009:0> Prime.prime?(10)
=> false
irb(main):010:0> Prime.prime?(13)
=> true
Workden, MNR PRIDE, 14, HAL Old Airport Rd, Domlur I Stage, 1st Stage, DOMLUR, Bengaluru, Karnataka 560071 [email protected] +91 95911 47880
Find us online

ROADMAPS

Learn Python
Learn Java
Learn C
Learn C++
Data structures and Algorithms
Competitive Programming
More Roadmaps

CAREER PATHS

React JS Developer
Full stack Developer
SQL for Data Analysis
Frontend Developer
Java Backend Developer
Data Analysis using Python
Python Backend Developer
C++ Developer
Machine Learning using Python

COMPILERS

HTML online compiler
C++ online compiler
C online compiler
Java online compiler
Python online compiler
SQL online compiler
JavaScript online compiler
React online compiler
More compilers

COMPANY

About us
For colleges
Coding Contests
Blogs
Contact us
Privacy Policy
Frequently Asked Questions

© 2025 CodeChef Inc. All rights reserved.

We use cookies to improve your experience and for analytical purposes. Read our Privacy Policy and Terms to know more. You consent to our cookies if you continue to use our website.