Ruby Interview Questions and Answers
What is Ruby?
- Ruby is an open-source, high-level programming language designed for simplicity and productivity. It has an elegant syntax that is easy to read and write, making it a popular choice for web development, especially with the Ruby on Rails framework.
What are the key features of Ruby?
- Object-oriented: Everything in Ruby is an object, including primitive data types.
- Dynamic typing: Ruby determines the type of variables at runtime.
- Garbage collection: Ruby automatically manages memory by cleaning up unused objects.
- Flexible and extensible: Ruby is designed to be flexible and allows you to add or modify functionality easily.
- Block-based syntax: Ruby supports closures and blocks for higher-order programming.
What is Ruby on Rails?
- Ruby on Rails (RoR) is a web application framework built on the Ruby programming language. It follows the Model-View-Controller (MVC) architecture and emphasizes convention over configuration, making web development fast and efficient.
What is the difference between `nil` and `false` in Ruby?
- In Ruby,
nil
is used to represent "nothing" or the absence of a value, whilefalse
represents a boolean value indicating "no" or "false" in logical operations. Both are considered "falsy" values, but they are not the same.
What are blocks in Ruby?
- Blocks in Ruby are anonymous functions or pieces of code that can be passed to methods to execute. They are similar to closures in other languages and are often used with iterators or methods like
each
.
What is the difference between a symbol and a string in Ruby?
- In Ruby, strings are mutable sequences of characters, whereas symbols are immutable, lightweight identifiers often used as keys in hashes. Symbols are more memory-efficient than strings when used repeatedly.
What is the use of `self` in Ruby?
self
refers to the current object in Ruby. It is used to access instance variables, call instance methods, and define class-level methods. In a class,self
refers to the class itself, and inside an instance method, it refers to the object.
What are the different types of variables in Ruby?
Local Variables
: Variables defined within a method or block.Instance Variables
: Variables prefixed with@
that hold data for an object.Class Variables
: Variables prefixed with@@
that are shared across all instances of a class.Global Variables
: Variables prefixed with$
that can be accessed from anywhere in the program.Constants
: Variables written in uppercase letters, typically used for values that shouldn't change.
What are iterators in Ruby?
- Iterators are methods that allow you to traverse through collections like arrays and hashes. Common iterators in Ruby include
each
,map
,select
, andreject
.
What are mixins in Ruby?
- Mixins are modules that provide functionality to classes without using inheritance. Ruby allows you to include modules in classes using the
include
orextend
keywords, enabling code reuse.
What is the purpose of the `require` and `load` methods in Ruby?
require
is used to load external libraries or Ruby files, and it ensures that files are only loaded once.load
is similar but reloads files every time it is called.
What is a Gem in Ruby?
- A Gem is a package or library in Ruby that can be shared and reused. Gems can be installed via the
gem
command and are stored in the RubyGems repository.
What is the purpose of the `initialize` method in Ruby?
- The
initialize
method is a special instance method in Ruby that gets called when a new object is instantiated. It is used to set up initial values or perform other setup tasks for the object.
What are regular expressions in Ruby?
- Regular expressions (regex) in Ruby are patterns used to match strings. Ruby provides the
Regexp
class to work with regular expressions, allowing you to perform search, replace, and validation tasks.
What is Duck Typing in Ruby?
- Duck Typing is a concept in Ruby where the type or class of an object is determined by its behavior rather than its inheritance. If an object behaves like a specific type, it can be used in that context.
What is the `attr_accessor`, `attr_reader`, and `attr_writer` in Ruby?
attr_accessor
generates both getter and setter methods for an instance variable.attr_reader
generates only a getter method.
attr_writer
generates only a setter method.
What is the difference between `==` and `equal?` in Ruby?
==
checks for value equality, whileequal?
checks for object identity, i.e., whether two variables point to the same object in memory.
What is a Proc in Ruby?
- A Proc is an object that holds a block of code that can be called at a later time. Procs can be passed as arguments, stored in variables, and invoked using the
call
method.
What is the difference between a block, Proc, and lambda in Ruby?
- A block is an anonymous piece of code passed to a method. A
Proc
is a block that has been converted into an object. Alambda
is a special kind of Proc that behaves like a method, checking the number of arguments and returning control when done.
What is the `super` keyword in Ruby?
super
is used to call the same method in the superclass (parent class). It can be used with or without arguments to invoke the parent method from a subclass.
What is the `yield` keyword in Ruby?
yield
is used to transfer control from a method to a block. It allows you to pass execution from a method to the block that was given to the method when it was called.
What is the `Module` class in Ruby?
- A
Module
in Ruby is a collection of methods and constants that can be mixed into classes. It provides the functionality of mixins, enabling code reuse across multiple classes.
What is the difference between class and module in Ruby?
- Classes are blueprints for creating objects, while modules are used to define reusable code that can be mixed into classes. Modules cannot be instantiated, but classes can.
What is an Enumerator in Ruby?
- An Enumerator is an object that allows you to iterate over a collection. It can be used with methods like
each
,map
, andcollect
.
What is a Hash in Ruby?
- A
Hash
is a collection of key-value pairs. It is similar to a dictionary in other languages and is used to store data in an unordered way.
What is `nil` in Ruby?
nil
is a special object in Ruby that represents "nothing" or "no value." It is often used to signify the absence of a value.
How do you handle exceptions in Ruby?
- Exceptions in Ruby can be handled using
begin
,rescue
,ensure
, andelse
blocks. Thebegin
block contains code that might raise an exception, and therescue
block catches the exception.
What is the `at_exit` method in Ruby?
at_exit
registers a block to be executed when the Ruby interpreter exits, making it useful for cleanup tasks.
How do you create a thread in Ruby?
- You can create a new thread in Ruby using the
Thread.new
method. Threads allow concurrent execution of code in separate threads, improving performance in I/O-bound applications.
What is the `__FILE__` and `__LINE__` in Ruby?
__FILE__
returns the name of the current source file, and__LINE__
returns the current line number in the file.
What are some ways to optimize Ruby code?
- Some optimizations in Ruby include using efficient algorithms, minimizing object creation, utilizing lazy enumerators, and profiling code to identify bottlenecks.