Online Ruby Compiler

Write, run, and debug Ruby code online in a full-featured browser IDE. Work with multiple files and gems, save your workspace, and collaborate in real time.

🚀 233,813 total executions (97 this month)
Embed
Output

Practice Python with coding challenges

Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.

All Python challenges
Easy

Rectangle Perimeter

Compute the perimeter of a rectangle from its sides.

You'll learn ArithmeticReturn values
BeginnerLearning-pathVariables
Easy

Days in a Month

Return how many days a month has, including leap years.

You'll learn Calendar rulesHelper functions
BeginnerConditionsFunctionsLearning-path
Easy

Dot Product of Two Vectors

Multiply two vectors element by element and add the results: the core operation of every neuron.

You'll learn VectorsDot product
Neural networksaimachine learning
Easy

Distance Between Points

Compute the straight-line distance between two points.

You'll learn Pythagorean theoremSquare roots
BeginnerFunctionsLearning-pathmath
Easy

Average of Three

Average three whole numbers.

You'll learn ArithmeticDivision
BeginnerLearning-pathVariables
Easy

To-Do List

Keep a list of tasks: add them, mark them done, remove them and report what is still pending.

You'll learn Typed collectionsRecords
BeginnerSimulationStrings
Easy

Prime Number Check

Decide whether a number is prime.

You'll learn DivisibilityLoop optimization
BeginnerLoopsNumbersStudent-beginner
Hard

Word Break

Decide whether text can be split into dictionary words.

You'll learn Prefix DPString segmentation
AdvancedDynamic-programmingHackathonStrings
Easy

Count Pairs With a Sum

Count pairs of positions whose values add up to a target.

You'll learn Pairs of indexesAvoiding double counts
ArraysBeginnerLearning-pathNested-loops
Easy

Missing Number

Find the one number missing from 0..n.

You'll learn Arithmetic seriesSum difference trick
ArraysBeginnerStudent-beginnermath
Medium

K-th Smallest Element

Find the k-th smallest value without fully sorting.

You'll learn QuickselectPartitioning
AdvancedCompetitive-programmingSorting
Easy

Min and Max Together

Find the smallest and largest value in one pass.

You'll learn Single passMultiple return values
ArraysBeginnerFunctionsLearning-path

💎 About This Ruby Online Executor

The CodeUtility Ruby Executor allows you to write, run, and test Ruby code instantly in your browser - no installation or setup needed. It runs in secure, isolated environments powered by real Ruby interpreters, supporting versions 2.7, 3.1, and 3.2.

Whether you're practicing Ruby fundamentals, testing algorithms, or learning object-oriented programming, this tool provides a simple and interactive console for immediate feedback.

You can use it to explore Ruby syntax, play with gems, or debug short snippets without configuring a local environment. It’s ideal for learners, backend developers, and anyone experimenting with Ruby-based logic.

All executions are processed in sandboxed environments to ensure safety and reliability while preserving real Ruby behavior.

More than a code runner

Use a multi-file editor, an isolated execution terminal, runtime-specific libraries, persistent workspaces, sharing, and real-time collaboration from the same executor.

How to use this executor?

Choose a runtime, open or create a file, write your code, and select Run. Output appears in the terminal, while signed-in users can keep files in a workspace, install libraries, and collaborate through shares.

  1. Select a version and edit the current file or open another workspace file.
  2. Run the code, provide input in the terminal when requested, and review saved run history.
  3. Use libraries, workspace tools, sharing, and chat when your task needs more than one snippet.

💡 Ruby Basics Guide for Beginners

1. Declaring Variables and Constants

Ruby is dynamically typed. Constants start with uppercase letters and are not meant to change.

x = 10
pi = 3.14
name = "Alice"
is_active = true

MAX_USERS = 100
APP_NAME = "CodeUtility"

2. Conditionals (if / case)

Use if, elsif, else, or case for control flow.

x = 2
if x == 1
  puts "One"
elsif x == 2
  puts "Two"
else
  puts "Other"
end

case x
when 1
  puts "One"
when 2
  puts "Two"
else
  puts "Other"
end

3. Loops

Use while, until, or iterators like each.

i = 0
while i < 3
  puts i
  i += 1
end

[1, 2, 3].each do |n|
  puts n
end

4. Arrays

Arrays store ordered lists of elements. Access using indexes.

fruits = ["apple", "banana", "cherry"]
puts fruits[0]
puts fruits.length

5. Array Manipulation

Use push, pop, slice, and reverse for working with arrays.

fruits.push("kiwi")
fruits.pop
puts fruits[1..2]
puts fruits.reverse

# Array comprehension
squares = (1..5).map { |x| x * x }
puts squares

6. Console Input/Output

Use gets.chomp to read input and puts/print for output.

print "Enter your name: "
name = gets.chomp
puts "Hello, #{name}"

7. Functions

Define functions using def. You can pass arguments and return values.

def greet(name)
  "Hello, #{name}"
end

puts greet("Alice")

8. Hashes

Hashes are key-value pairs, like dictionaries or maps.

person = { "name" => "Bob", "age" => 25 }
puts person["name"]

# Symbol keys
person = { name: "Alice", age: 30 }
puts person[:name]

9. Exception Handling

Use begin-rescue-end to catch exceptions and handle errors gracefully.

begin
  raise "Something went wrong"
rescue => e
  puts e.message
end

10. File I/O

Read and write files using File methods or IO classes.

File.write("file.txt", "Hello File")
content = File.read("file.txt")
puts content

11. String Manipulation

Ruby strings support many methods: length, gsub, split, etc.

text = " Hello World "
puts text.strip
puts text.upcase
puts text.gsub("Hello", "Hi")
puts text.split

12. Classes & Objects

Ruby is fully object-oriented. Use initialize to define constructors.

class Person
  def initialize(name)
    @name = name
  end

  def greet
    "Hi, I'm #{@name}"
  end
end

p = Person.new("Alice")
puts p.greet

13. References (Object Mutation)

All variables hold references to objects. Modifying an object inside a function affects the original.

def modify(arr)
  arr << "changed"
end

data = ["original"]
modify(data)
puts data.inspect  # ["original", "changed"]