Ruby Basics:
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.
Explain the difference between
puts
,print
, andp
in Ruby.- Answer:
puts
adds a newline after output,print
does not add a newline, andp
is used for debugging and provides a more detailed output.
- Answer:
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.
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.
- Answer: Local variables (e.g.,
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:
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.
- Answer: You can use the
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.
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.
- Answer: The
How do you remove an element from an array in Ruby?
- Answer: You can use methods like
pop
,shift
, ordelete_at
to remove elements from an array.
- Answer: You can use methods like
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.
- Answer: The
Object-Oriented Programming (OOP) in Ruby:
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.
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).
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.
- Answer:
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
.
- Answer: Objects are created by instantiating a class using the
What is the difference between
attr_reader
,attr_writer
, andattr_accessor
?- Answer:
attr_reader
creates a getter method,attr_writer
creates a setter method, andattr_accessor
creates both getter and setter methods for a class attribute.
- Answer:
Blocks, Procs, and Lambdas:
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.
- Answer: A block is a chunk of code enclosed within
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.
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 (&
).
- Answer: You can pass a block to a method by using the
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.
- Answer:
How do you handle exceptions in Ruby?
- Answer: Exceptions are handled using a
begin..rescue..end
block. Therescue
block catches exceptions and allows you to handle or log the error.
- Answer: Exceptions are handled using a
Modules and Mixins:
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.
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.
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.
- Answer: You can use the
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.
- Answer: The
Explain the difference between
include
andextend
in Ruby.- Answer:
include
is used to add module methods to a class, making them available to instances, whileextend
adds module methods to a single object.
- Answer:
Ruby on Rails:
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.
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.
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.
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.
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:
- 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.
Explain the purpose of the
before
andafter
hooks in RSpec.- Answer:
before
andafter
hooks in RSpec allow you to run code before and after each example or group of examples, providing setup and teardown functionality.
- Answer:
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.
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.
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
.
- Answer: Tests are run using the
Metaprogramming in Ruby:
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.
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.
- Answer:
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.
- Answer: The
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.
- Answer: The
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:
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.
How do you install a Ruby Gem?
- Answer: Gems can be installed using the
gem install
command. For example,gem install gem_name
.
- Answer: Gems can be installed using the
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.
- Answer: The
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.
- Answer: Bundler is a tool for managing Ruby gem dependencies in a project. It reads the
How do you use a Ruby Gem in a project?
- Answer: Once a gem is specified in the
Gemfile
, runningbundle install
installs the gem, and you can require it in your Ruby code.
- Answer: Once a gem is specified in the
Miscellaneous:
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.
- Answer: The
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.
- Answer: The
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.
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.
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.
- Answer: Multiple lines can be commented out using