interview-questions
  1. ruby-interview-questions

Ruby Interview Questions & Answers


Ruby Basics:

  1. What is Ruby?

    • Answer: Ruby is a dynamic, object-oriented programming language designed for simplicity and productivity. It has an elegant syntax and is often used for web development.
  2. Explain the difference between puts, print, and p in Ruby.

    • Answer: puts adds a newline after output, print does not add a newline, and p is used for debugging and provides a more detailed output.
  3. What are symbols in Ruby?

    • Answer: Symbols are immutable identifiers used to represent names and strings. They are often used as keys in hashes for efficiency.
  4. Differentiate between a local variable and an instance variable.

    • Answer: Local variables (e.g., x) have limited scope within a block, method, or class, while instance variables (e.g., @x) are available throughout an object's lifetime.
  5. Explain the concept of duck typing in Ruby.

    • Answer: Duck typing is a dynamic typing concept where the type or class of an object is determined by its behavior (methods and properties) rather than its explicit type.

Arrays and Hashes:

  1. How do you add an element to an array in Ruby?

    • Answer: You can use the push method or the shovel (<<) operator to add elements to an array.
  2. Explain the difference between an array and a hash in Ruby.

    • Answer: An array is an ordered collection of elements accessed by index, while a hash is an unordered collection of key-value pairs.
  3. What is the purpose of the each method in Ruby?

    • Answer: The each method is used for iterating over elements in a collection (array, hash, etc.) and executing a block of code for each element.
  4. How do you remove an element from an array in Ruby?

    • Answer: You can use methods like pop, shift, or delete_at to remove elements from an array.
  5. Explain the purpose of the map method in Ruby.

    • Answer: The map method is used to transform each element of an enumerable object (e.g., array) based on the provided block and return a new array with the transformed values.

Object-Oriented Programming (OOP) in Ruby:

  1. What is a class in Ruby?

    • Answer: A class is a blueprint for creating objects in Ruby. It defines attributes and behaviors that objects created from the class will have.
  2. Explain the concept of inheritance in Ruby.

    • Answer: Inheritance allows a class (subclass or child class) to inherit attributes and behaviors from another class (superclass or parent class).
  3. What is the purpose of the attr_accessor in Ruby?

    • Answer: attr_accessor is a shortcut in Ruby for defining getter and setter methods for class attributes.
  4. How do you create an object in Ruby?

    • Answer: Objects are created by instantiating a class using the new method. For example, obj = ClassName.new.
  5. What is the difference between attr_reader, attr_writer, and attr_accessor?

    • Answer: attr_reader creates a getter method, attr_writer creates a setter method, and attr_accessor creates both getter and setter methods for a class attribute.

Blocks, Procs, and Lambdas:

  1. What is a block in Ruby?

    • Answer: A block is a chunk of code enclosed within do..end or curly braces {}. It can be passed to methods and executed.
  2. Explain the difference between a Proc and a Lambda.

    • Answer: Both are objects representing blocks of code. The key difference is how they handle the return keyword and the number of arguments they expect.
  3. How can you pass a block to a method in Ruby?

    • Answer: You can pass a block to a method by using the yield keyword or by explicitly passing it as a parameter preceded by an ampersand (&).
  4. What is the purpose of the each_with_index method in Ruby?

    • Answer: each_with_index is an Enumerable method that iterates over elements, providing both the element and its index to the block.
  5. How do you handle exceptions in Ruby?

    • Answer: Exceptions are handled using a begin..rescue..end block. The rescue block catches exceptions and allows you to handle or log the error.

Modules and Mixins:

  1. What is a module in Ruby?

    • Answer: A module is a way to encapsulate methods, constants, and classes in Ruby. Modules cannot be instantiated but can be included in classes for code reuse.
  2. Explain the concept of a mixin in Ruby.

    • Answer: A mixin is a way to include the methods and behaviors of a module into a class. It allows for code reuse without using traditional inheritance.
  3. How do you include a module in a class in Ruby?

    • Answer: You can use the include keyword followed by the module name to include a module in a class.
  4. What is the purpose of the extend keyword in Ruby?

    • Answer: The extend keyword is used to add module methods to a single object (instance) rather than to an entire class.
  5. Explain the difference between include and extend in Ruby.

    • Answer: include is used to add module methods to a class, making them available to instances, while extend adds module methods to a single object.

Ruby on Rails:

  1. What is Ruby on Rails?

    • Answer: Ruby on Rails, often called Rails, is a web application framework written in Ruby. It follows the Model-View-Controller (MVC) architecture and emphasizes convention over configuration.
  2. What is ActiveRecord in Ruby on Rails?

    • Answer: ActiveRecord is the Object-Relational Mapping (ORM) component in Ruby on Rails, providing an interface between the application and the database.
  3. Explain the purpose of migrations in Ruby on Rails.

    • Answer: Migrations in Rails are used to alter the database schema over time. They allow developers to make changes to the database structure and data.
  4. What is the Rails asset pipeline?

    • Answer: The asset pipeline in Rails is a system that manages and processes static assets such as stylesheets and JavaScript files. It helps optimize asset delivery.
  5. Explain the concept of RESTful routing in Ruby on Rails.

    • Answer: RESTful routing in Rails follows the principles of Representational State Transfer (REST) and defines standard routes for creating, reading, updating, and deleting resources.

Testing in Ruby:

  1. What is RSpec in Ruby?
    • Answer: RSpec is a testing framework for Ruby that allows developers to

write tests in a clear and expressive manner using a behavior-driven development (BDD) approach.

  1. Explain the purpose of the before and after hooks in RSpec.

    • Answer: before and after hooks in RSpec allow you to run code before and after each example or group of examples, providing setup and teardown functionality.
  2. What is TDD (Test-Driven Development)?

    • Answer: Test-Driven Development is a development approach where tests are written before the actual code. Developers write tests to define the desired functionality and then write code to make the tests pass.
  3. What is BDD (Behavior-Driven Development)?

    • Answer: Behavior-Driven Development is an extension of TDD that emphasizes collaboration between developers, QA, and non-technical stakeholders. It uses natural language specifications for defining behavior.
  4. How do you run tests in Ruby using RSpec?

    • Answer: Tests are run using the rspec command followed by the file or directory containing the tests. For example, rspec spec/models/user_spec.rb.

Metaprogramming in Ruby:

  1. What is metaprogramming in Ruby?

    • Answer: Metaprogramming in Ruby involves writing code that writes or modifies other code at runtime. It allows for dynamic and flexible programming.
  2. Explain the purpose of method_missing in Ruby.

    • Answer: method_missing is a method called by Ruby when a method is invoked that the object doesn't respond to. It can be used for dynamic method handling.
  3. What is the purpose of the send method in Ruby?

    • Answer: The send method is used to invoke a method dynamically on an object. It takes the method name as a symbol and optional arguments.
  4. What is the eval method in Ruby used for?

    • Answer: The eval method in Ruby is used to evaluate a string as Ruby code. It allows dynamic code execution.
  5. Explain the concept of Open Classes in Ruby.

    • Answer: Open Classes in Ruby allow you to reopen and modify existing classes at runtime. This feature enables developers to add or override methods.

Ruby Gems:

  1. What is a Ruby Gem?

    • Answer: A Ruby Gem is a package manager for distributing and managing Ruby libraries and programs. Gems can be installed and included in Ruby projects.
  2. How do you install a Ruby Gem?

    • Answer: Gems can be installed using the gem install command. For example, gem install gem_name.
  3. Explain the purpose of the Gemfile in Ruby.

    • Answer: The Gemfile in Ruby is used to specify the gems and their versions required for a particular project. It is used with the Bundler tool for dependency management.
  4. What is Bundler in Ruby?

    • Answer: Bundler is a tool for managing Ruby gem dependencies in a project. It reads the Gemfile and installs the specified gems along with their dependencies.
  5. How do you use a Ruby Gem in a project?

    • Answer: Once a gem is specified in the Gemfile, running bundle install installs the gem, and you can require it in your Ruby code.

Miscellaneous:

  1. Explain the concept of the super keyword in Ruby.

    • Answer: The super keyword is used to call the same method in the superclass. It is often used to extend or override functionality in the subclass.
  2. What is the purpose of the yield keyword in Ruby?

    • Answer: The yield keyword is used to call a block passed to a method. It allows the method to execute the block of code provided by the caller.
  3. Explain the concept of the spaceship (<=>) operator in Ruby.

    • Answer: The spaceship operator is a comparison operator that returns -1, 0, or 1, indicating whether the left operand is less than, equal to, or greater than the right operand.
  4. What is a lambda in Ruby?

    • Answer: A lambda is a way to create an anonymous function in Ruby. It is similar to a proc but has a stricter argument checking.
  5. How do you comment out multiple lines in Ruby?

    • Answer: Multiple lines can be commented out using =begin and =end or by adding a # at the beginning of each line.