
Ruby Interview Questions and Answers
Top 100 Ruby Interview Questions for Freshers
Ruby is one of the most widely used programming languages in top tech companies, including IDM TechPark. Its flexibility, simplicity, and extensive libraries make it an essential skill for developers. To secure a Ruby developer role at IDM TechPark, candidates must be well-versed in Ruby concepts and ready to tackle both the Ruby Online Assessment and Technical Interview Round.
To help you succeed, we have compiled a list of the Top 100 Ruby Interview Questions along with their answers. Mastering these will give you a strong edge in cracking Ruby interviews at IDM TechPark.
1. What is Ruby?
Answer:
Ruby is a dynamic, open-source, object-oriented programming language known for its simplicity and readability. It is widely used in web development, scripting, and automation.
2. What are the main features of Ruby?
Answer:
-
Purely object-oriented (everything is an object).
-
Dynamic typing and duck typing.
-
Garbage collection for memory management.
-
Metaprogramming support.
-
Blocks, Procs, and Lambdas for flexible coding.
3. What is IRB in Ruby?
Answer:
IRB (Interactive Ruby) is a command-line tool that allows you to execute Ruby code interactively.
sh
CopyEdit
$ irb >> puts "Hello, Ruby!" Hello, Ruby!
4. What are Ruby’s primitive data types?
Answer:
-
Integer (10, -5)
-
Float (3.14, -2.5)
-
Boolean (true, false)
-
String ("Hello", 'World')
-
Symbol (:name, :age)
-
Array ([1, 2, 3])
-
Hash ({name: "John", age: 25})
5. What is the difference between puts, print, and p in Ruby?
Answer:
-
puts adds a newline after output.
-
print does not add a newline.
-
p prints debug-friendly output (inspects objects).
ruby
CopyEdit
puts "Hello" # Output: Hello (newline) print "World" # Output: World (no newline) p [1, 2, "Hi"] # Output: [1, 2, "Hi"]
6. What is a Symbol in Ruby?
Answer:
A Symbol is an immutable, unique identifier used as a lightweight alternative to strings.
ruby
CopyEdit
:name == :name # true (same object) "hello" == "hello" # true (but different objects)
7. What is the difference between an Array and a Hash in Ruby?
Answer:
-
Array: Ordered collection of elements.
ruby
CopyEdit
arr = [1, 2, 3]
-
Hash: Key-value pair storage.
ruby
CopyEdit
hash = { name: "John", age: 25 }
8. What is the difference between ==, .eql?, and equal? in Ruby?
Answer:
-
== checks value equality.
-
.eql? checks value & type equality.
-
equal? checks object identity.
ruby
CopyEdit
a = "hello" b = "hello" c = a a == b # true a.eql? b # true a.equal? b # false a.equal? c # true
9. What is the difference between a Block, Proc, and Lambda in Ruby?
Answer:
-
Block: A piece of code passed to methods.
-
Proc: A saved block that can be reused.
-
Lambda: Similar to Proc but has stricter argument handling.
ruby
CopyEdit
def greet yield end greet { puts "Hello!" } # Block proc_example = Proc.new { puts "Hello from Proc!" } proc_example.call # Proc lambda_example = -> { puts "Hello from Lambda!" } lambda_example.call # Lambda
10. What is a Class in Ruby?
Answer:
A class is a blueprint for creating objects.
ruby
CopyEdit
class Car def initialize(model) @model = model end end
11. What are blocks in Ruby?
Answer:
Blocks are chunks of code enclosed in {} or do...end that can be passed to methods.
def greet yield end greet { puts "Hello from the block!" }
12. What is the difference between puts, print, and p in Ruby?
Answer:
MethodDescription
putsAdds newline after output
printOutputs without newline
pShows raw form (for debugging)
13. What are symbols in Ruby?
Answer:
Symbols are immutable, reusable constants. They start with a colon (:).
:name, :id
They use less memory than strings and are often used as hash keys.
14. What is the difference between == and === in Ruby?
Answer:
-
== checks value equality
-
=== is used for case equality, mainly in case...when statements
case 5 when Integer puts "It's an integer" # Uses === internally end
15. How are hashes defined in Ruby?
Answer:
person = { name: "Alice", age: 25 }
Hashes use key-value pairs. Keys can be symbols, strings, or objects.
16. What is the difference between require and include in Ruby?
Answer:
-
require: Loads external files/libraries
-
include: Mixes a module's methods into a class
require 'date' include Math
17. What is the self keyword in Ruby?
Answer:
self refers to the current object. In a class, it can be used to define class methods or access the instance itself.
class Person def self.say_hi puts "Hi!" end end
18. What is a module in Ruby?
Answer:
Modules are collections of methods and constants. Used for namespacing and mixins.
module Greet def hello "Hello!" end end
Use include or extend to use them in classes.
19. What is an iterator in Ruby?
Answer:
An iterator is a method that repeatedly invokes a block. Common ones: each, map, select.
[1, 2, 3].each { |n| puts n }
20. How do you define a class and create an object in Ruby?
Answer:
class Dog def bark "Woof!" end end my_dog = Dog.new puts my_dog.bark
​
21. Q: What is Ruby?
A: Ruby is a high-level, interpreted, object-oriented programming language known for its simplicity and productivity. It has an elegant syntax that is easy to read and write.
22. Q: What are variables in Ruby and how are they declared?
A: Variables in Ruby are used to store data. They are dynamically typed and declared by simply assigning a value.
Example:
name = "Alice" age = 30
23. Q: What is a method in Ruby?
A: A method is a block of code that performs a specific task and can be reused. It is defined using the def keyword.
Example:
def greet(name) "Hello, #{name}!" end puts greet("Bob")
24. Q: What are arrays in Ruby?
A: Arrays are ordered collections that can hold objects of any type.
Example:
fruits = ["apple", "banana", "cherry"] puts fruits[1] # Output: banana
25. Q: What is the difference between puts and print in Ruby?
A:
-
puts outputs text with a newline at the end.
-
print outputs text without adding a newline.
Example:
puts "Hello" print "World" # Output: # Hello # World
​​