
Ruby
Basic Interview Q&A
1. What is Ruby and why is it considered an object-oriented language (OOP)?
Ruby is a dynamic, high-level programming language known for its simplicity and elegance. Ruby is considered an object-oriented programming (OOP) language because everything in Ruby is treated as an object. Each value in Ruby, be it a number, a string, an array, or a hash, is an object with its own methods and attributes. This object-oriented nature allows developers to model real-world entities and interactions more naturally through classes and objects.
In Ruby, even classes and modules are objects, which is a characteristic not found in all OOP languages. This means you can dynamically alter classes and modules at runtime, providing a high degree of flexibility.
The object-oriented nature of Ruby also promotes encapsulation, inheritance, and polymorphism, which are key principles of OOP. These principles make it easier to manage and manipulate data, enhance code reusability, and provide a structure that makes code easier to maintain and understand.
2. Differentiate between local variables and instance variables.
Local variables in Ruby are variables that are defined within a method or a block. They are confined to the scope in which they are declared and are not accessible outside of it. This means that if you define a local variable inside a method, it cannot be accessed outside of that method.
On the other hand, instance variables, denoted with an '@' prefix, belong to a specific instance of a class. They enable data persistence and access across different methods within that instance. This means that the value of an instance variable persists as long as the object instance exists.
3. How do you define a method in Ruby?
In Ruby, a method is defined using the def keyword, followed by the method name. If the method takes parameters, they are placed in parentheses after the method name. The body of the method, which contains the code to be executed when the method is called, follows next. The method definition is concluded with the end keyword.
It's important to note that in Ruby, if the last statement of a method is an expression, its value will be returned by the method. This means you don't need to use the return keyword explicitly, although you can if you want to return early from a method.
4. Explain the concept of symbols in Ruby.
Symbols in Ruby are lightweight, immutable objects that are often used as identifiers or keys. They are denoted by a colon followed by a name, like :symbol_name.
Unlike strings, which can be changed (or mutated) during the execution of a program, symbols are immutable. This means that once a symbol is created, it cannot be changed. This immutability makes symbols efficient for comparison operations, as two symbols with the same name are always identical, whereas two strings with the same content may not be.
Symbols are often used as keys in hashes because they are more memory-efficient than strings. When you use a symbol multiple times, it refers to the same object in memory, whereas each usage of a string with the same content refers to a different object.
5. Why is Ruby known as a language of flexibility?
This is an important Ruby interview question. Ruby is recognized as a flexible language since it allows its author to change the programming parts. Some aspects of the language can be deleted or redefined. Ruby does not impose any limitations on the user. To add two numbers, for example, Ruby allows you to use the + sign or the word 'plus'. This modification is possible using Ruby's built-in class Numeric.
6. What is the purpose of the 'nil' value in Ruby?
In Ruby, nil is a special object used to represent the absence of a value or an undefined state. It is the sole instance of the NilClass class.
nil is often used to indicate that a method has not returned a meaningful result. For example, if you try to retrieve a value from a hash using a key that doesn't exist, the method will return nil:
my_hash = { name: "Alice" }
puts my_hash[:age] # Outputs: nil
In this example, because there is no :age key in my_hash, the expression my_hash[:age] returns nil.
7. How can you add a comment in Ruby code?
In Ruby, you can add comments to your code to provide explanations or annotations. Comments are ignored by the Ruby interpreter and do not affect the execution of the program.
Single-line comments are created by starting a line with the # symbol. Everything after the # symbol on that line is considered a comment:
#This is a single-line comment in Ruby
puts "Hello, World!" # You can also add a comment after code on the same line
For multi-line comments, Ruby provides a =begin and =end syntax, but this is rarely used and not recommended because it can conflict with the RDoc documentation system. Instead, it's common to simply use the # symbol at the start of each line:

In this example, the first three lines are all comments and will be ignored by the Ruby interpreter.
8. What is the use of load and required?
Load and need are both used in Ruby to load available code into the current code. It is recommended to use 'load' when the code must be loaded every time it is altered or when someone visits the URL. It is recommended to use 'require' in the case of autoload.
9. Describe the difference between 'puts' and 'print' statements.
In Ruby, both puts and print are used to output data to the console, but they behave slightly differently.
puts (short for "put string") outputs the data you give it followed by a newline character (\n), which moves the cursor to the next line. This means that every call to puts will begin on a new line. It's ideal for displaying messages or data where you want each piece of output to appear on a separate line.
On the other hand, print outputs the data you give it exactly as is, without adding a newline character. This means that unless you specifically include a newline character (\n) in your output, print continues printing on the same line. This can be useful if you want to format your output more precisely or if you want to display output on the same line as input.
10. What is the difference between the Observers and Callbacks?
Rails Observers: Similar to Callback, Observers are used when the method is not directly related to the object lifecycle. In addition, the observer has a longer lifespan and can be detached or attached at any time. For example, displaying model values in the UI and updating the model based on user input.
Rail Callback: This method can be called at specific points in an object's life cycle, such as when an object is validated, created, updated, or removed. A callback is a short-lived method. For example, consider operating a thread and providing a call-back function that is invoked after the thread terminates.
11. What are the naming conventions?
This is an important Ruby interview question.
- Variables: All letters are lowercase while declaring Variables, and words are separated by underscores.
- Module and Class: Modules and Classes are written in MixedCase with no underscores; each word begins with an uppercase letter.
- The table in the database: The database table name should have lowercase letters and underscores between words, and all table names should be plural, for example, invoice items.
- Model: It is represented as unbroken MixedCase with singular with the table name.
- Controller: Controller class names are written in plural form, so OrdersController is the controller for the order table.
12. How do you create an array in Ruby?
In Ruby, an array is an ordered collection of elements. You can create an array by enclosing a comma-separated list of elements in square brackets ([]). For example:
my_array = [1, 2, 3]
In this example, my_array is an array containing three elements: 1, 2, and 3.
Arrays in Ruby are indexed starting from 0, which means the first element is at index 0, the second element is at index 1, and so on. You can access elements in an array by their index:

In Ruby, arrays can contain elements of different types. This means you can have an array that contains a mix of numbers, strings, and other objects:
mixed_array = [1, "two", :three]
In this example, mixed_array is an array containing a number, a string, and a symbol.
13. Explain the usage of the 'if' statement in Ruby.
The if statement in Ruby is used for conditional branching in code. It evaluates a condition and, if the condition is true, it executes a block of code. Here's a basic example:

In this example, the if statement checks whether number is greater than 5. Because number is 10, which is indeed greater than 5, the code within the if block is executed, and "The number is greater than 5" is printed to the console.
You can also use the else keyword to specify a block of code to be executed if the condition is false:

14. What is a range and how is it defined in Ruby?
A range in Ruby represents a sequence of values, typically numeric or alphabetic. Ranges can be used in case statements, for loop iteration, or to create an array of sequential elements.
Ranges can be defined in two ways:
- Inclusive ranges, denoted by two dots (..), include the end value in the range. For example, 1..5 represents the numbers 1 through 5, inclusive.
inclusive_range = 1..5
puts inclusive_range.to_a # Outputs: [1, 2, 3, 4, 5]
- Exclusive ranges, denoted by three dots (...), exclude the end value from the range. For example, 1...5 represents the numbers 1 through 4.
exclusive_range = 1...5
puts exclusive_range.to_a # Outputs: [1, 2, 3, 4]
In these examples, to_a is a method that converts the range to an array, and puts outputs the array to the console.
15. How is Symbol different from variables?
In the following ways, the symbol differs from the variables.
- It is more akin to a string than a variable.
- A string in Ruby is mutable, while a symbol is not.
- There is only one duplicate of the symbol that has to be produced.
- In Ruby, symbols are frequently used to correlate to enums.
16. How is exception handling implemented in Ruby?
Exception handling in Ruby is implemented using begin, rescue, and ensure blocks.
The begin block contains the code that might raise an exception. If an exception occurs within the begin block, execution immediately shifts to the rescue block.
The rescue block contains the code that handles the exception. You can have multiple rescue blocks to handle different types of exceptions. If an exception is raised but not rescued, the program terminates.
The ensure block contains code that will always be executed, whether an exception was raised or not. This is typically used for cleanup activities, like closing files or network connections.
17. Explain the principle of DRY (Don't Repeat Yourself) in Ruby.
DRY, or "Don't Repeat Yourself", is a fundamental principle in software development that aims to reduce repetition of code. The idea is to write code once and then reuse it, rather than duplicating it. This makes the code more maintainable, more readable, and less prone to errors.
In Ruby, there are several ways to adhere to the DRY principle:
- Methods: If you find yourself writing the same code in multiple places, consider moving that code into a method, which can then be called from those places.
- Classes and Objects: If you have a set of methods that operate on the same data, consider creating a class. The data can be stored in instance variables, and the methods can be defined as instance methods.
- Modules: If you have methods that can be used across different classes, consider defining them in a module, which can then be mixed into those classes.
18. What are the features of Rails?
Rails include numerous features such as:
- Meta-programming: Rails uses code generation, but meta-programming is used for heavy lifting. Ruby is regarded as one of the best meta-programming languages.
- Active Record: It uses the Active Record Framework to save objects to the database. Using metaprogramming, the Rails version of Active Record identifies the column in a schema and automatically connects it to your domain objects.
- Scaffolding: Rails may automatically generate scaffolding or interim code.
- Rails, unlike other development frameworks, do not require much configuration if you strictly adhere to the naming standard.
- Rails include three environments by default: testing, development, and production.
- Built-in testing: It supports code known as harness and fixtures, which allow test cases to be written and executed.
19. Differentiate between '==' and 'equal?' comparison operators.
== is the equality operator. It checks whether the values of two operands are equal or not. If yes, then the condition becomes true. It's important to note that == can be overridden by classes to provide class-specific definitions of equality. For example, the Array class overrides == to define equality as two arrays having the same elements in the same order.
On the other hand, equal? is the identity operator. It checks whether two operands refer to the exact same object in memory. Unlike ==, equal? cannot be overridden. This makes it useful for checking object identity, i.e., whether two variables refer to the exact same object.
20. List out what can Rails Migration do.
This is an important Ruby interview question. Rails Migration can do the following things:
- Create table
- Rename column
- Change column
- Remove column
- Drop table
- Rename table
- Add column
21. Explain the role of sub-directory app/controllers and app/helpers.
This is an important Ruby interview question.
- App/controllers: The Controller handles a user's web request. Rails look for controller classes in the controller subdirectory.
- App/helpers: Any helper classes needed to assist the view, model, and controller classes are stored in the helper's subdirectory.
22. What are modules and how are they different from classes?
Modules in Ruby are collections of methods, constants, and other module and class definitions. They provide a namespace and prevent name clashes, and they support the mixin facility for shared behavior.
Unlike classes, modules cannot be instantiated, which means you cannot create objects from a module. Also, a module cannot inherit from another module or class, and it cannot be the superclass of a class. In other words, modules do not participate in the class hierarchy.
23. How do you establish inheritance in Ruby classes?
In Ruby, inheritance is a relation between two classes where one class, the child (or subclass), inherits the attributes and behaviors of another class, the parent (or superclass). This allows you to create a general class first and then create more specialized classes later. Inheritance is established using the < symbol after the child class name, followed by the parent class name.
24. What is method overloading and does Ruby support it?
Method overloading involves defining multiple methods with the same name but different parameters. Ruby does not support method overloading in the traditional sense, but you can achieve similar results using default parameters and variable arguments.
25. What is Rake?
Rake is a Ruby Make; it is a Ruby utility that replaces the Unix utility 'make' and builds a list of tasks using a 'Rakefile' and '.rake files'. Rake is used in Rails for routine administration activities such as database migration via scripts, schema loading into the database, and so on.
26. Describe the concept of metaclasses (eigenclasses) in Ruby.
In Ruby, every object has a metaclass (also known as an eigenclass or singleton class). This is a special, hidden class that is specific to that object. The metaclass is part of Ruby's object model and is used to store singleton methods.
Singleton methods are methods that are defined on a single object, rather than on a class. This means they can be called on that object, but not on other objects of the same class.
27. Define the role of Rails Controller.
This is a common Ruby interview question. The Rails controller serves as the application's logical heart. It makes the interaction between users, views, and the model easier. It also does other tasks such as:
- It can route external requests to internal actions. It is highly adept at handling URLs. It governs helper modules, which extend the capabilities of view templates without bloating their code.
- It manages sessions, which give consumers the sense that they are interacting with our applications in real-time.
28. How can you include a module in a class in Ruby?
In Ruby, you can include a module in a class using the include keyword followed by the module name. This is known as a mixin. When a module is included in a class, the methods, constants, and other definitions from the module are mixed into that class.
When a module is included in a class, the module's methods are added as instance methods in the class. If you want to add the module's methods as class methods, you can use the extend keyword instead of include.
Including modules in classes is a way to share behavior among multiple classes without using inheritance. This is particularly useful in Ruby, which only supports single inheritance (a class can only inherit from one superclass).
29. Explain the singleton class and its use cases.
The singleton class is a design pattern that ensures a class has only one instance and provides a global point of access to it. It's useful for scenarios where you want to control access to a single instance, like a configuration manager. Singleton classes are useful in several scenarios:
- Adding behavior to individual objects: If you want to add a method to an individual object without affecting other objects of the same class, you can define a singleton method on that object.
- Class methods: In Ruby, class methods are actually singleton methods defined on the class object. When you define a class method, it is stored in the class's singleton class.
- Class macros: In Ruby, class macros like attr_accessor are implemented as singleton methods defined on the class object. When you call a class macro, it adds methods or variables to the class or its instances.
- Object-specific behavior: If you want an object to behave differently from other instances of its class, you can add methods to its singleton class. These methods will only affect that object, not other instances of the class.
30. Discuss the concept of blocks and their significance.
Blocks in Ruby are chunks of code enclosed between do..end or curly braces {..}. They can accept input and are used throughout Ruby, particularly with methods and iterators, as a way to pass around chunks of code. Blocks can be invoked from a function using the yield keyword.
Here's an example:

In this example, 3.times is a method call and everything between do and end is a block. The times method uses the block by calling it a certain number of times (in this case, three times), which results in "Hello, World!" being output to the console three times.
Blocks in Ruby can also take parameters. The parameters are defined between pipe characters (|) at the beginning of the block. For example:

In this example, the each method is called on an array and passes each element of the array to the block one at a time. The block takes one parameter, number, and outputs it to the console. This results in "Number: 1", "Number: 2", and "Number: 3" being output to the console.
Wrapping up
These questions provide applicants beginning their Ruby journey the ability to brush up on their technical knowledge as well as acquire an understanding of how Ruby concepts are used in real-world situations. These questions also offer hiring managers a methodical framework for objectively and successfully evaluating Ruby engineers.
Ultimately, the goal of any job interview is to find the perfect match – a candidate who aligns with the company's values and has the potential to make a meaningful impact. With an AI-backed vetting engine, Turing helps you achieve your desired results. Candidates from all over the world are assessed for their core competency in both technical and soft skills.
Hire Silicon Valley-caliber Ruby developers at half the cost
Turing helps companies match with top quality remote JavaScript developers from across the world in a matter of days. Scale your engineering team with pre-vetted JavaScript developers at the push of a buttton.
Tired of interviewing candidates to find the best developers?
Hire top vetted developers within 4 days.
Leading enterprises, startups, and more have trusted Turing
Check out more interview questions
Hire remote developers
Tell us the skills you need and we'll find the best developer for you in days, not weeks.





