Ruby CLI Playground Online

Simulate a Ruby terminal in your browser to test scripts and practice syntax quickly.

🚀 226,575 total executions (194 this month)

Udemy Logo Udemy Affiliates: Popular Ruby courses people love

Loading...

💎 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.

💡 How to Use This Tool

  • 1. Select a Ruby version from the dropdown above the editor (2.7, 3.1, or 3.2).
  • 2. Write or paste your Ruby code into the editor area.
  • 3. Click Run to execute your Ruby code and view the output in the console below.
  • 4. During execution, a Stop button appears - click it to halt the process early.
  • 5. Use Fix Code to automatically correct syntax or indentation issues.
  • 6. After fixing, a Fixes button will appear - click it to review recent code fixes.
  • 7. You can also Upload code from a file or Download your current code from the editor.
  • 8. Each run is limited to 20 seconds of execution time for safety and fairness.

🧠 Tip: The Ruby environment includes common standard libraries - ideal for experimenting with collections, classes, and methods right in your browser.

💡 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"]