[1] What is a block in Ruby and how is it different from a proc?
A block in Ruby is a piece of code that can be passed as an argument to a method. It is different from a proc (short for "procedure") in that a block is not an object and cannot be stored in a variable or passed around like a proc can. Here is an example of a block being used with the each method:
[1, 2, 3].each do |num|
puts num
end
[2] How do you define a method in Ruby?
To define a method in Ruby, you use the def keyword followed by the method name and any arguments the method takes. The method's code goes between the def and end keywords. For example:
def greet(name)
puts "Hello, #{name}!"
end
[3] What is the difference between an instance variable and a local variable in Ruby?
An instance variable in Ruby is a variable that is associated with a specific instance of an object. It starts with an @ symbol and is available to any methods defined within the object. Here is an example of using an instance variable:
class Dog
def initialize(name)
@name = name
end
def greet
puts "Hello, my name is #{@name}!"
end
end
dog = Dog.new("Fido")
dog.greet # outputs "Hello, my name is Fido!"
A local variable is a variable that is only available within the context in which it is defined, such as within a method or loop. It does not have the @ symbol at the beginning of its name.
[4] How do you create a new object in Ruby?
To create a new object in Ruby, you use the new method of the class you want to create an instance of. For example:
dog = Dog.new("Fido")
This creates a new Dog object with the name "Fido".
[5] What is the purpose of the each method in Ruby?
The each method in Ruby is used to iterate over a collection of items, such as an array or hash. It takes a block of code as an argument, and the block is executed once for each item in the collection. For example:
[1, 2, 3].each do |num|
puts num
end
This code will output the numbers 1, 2, and 3 on separate lines.
[6] How do you create a new class in Ruby and what is the syntax for doing so?
To create a new class in Ruby, you use the class keyword followed by the class name and any parent class it may inherit from. The class's code goes between the class and end keywords. For example:
class Dog
def initialize(name)
@name = name
end
def greet
puts "Hello, my name is #{@name}!"
end
end
[7] What is the difference between a symbol and a string in Ruby?
A symbol in Ruby is a representation of a name, similar to a string. However, symbols are immutable (meaning they cannot be changed) and are stored in a single memory location. This means that two symbols with the same name will be the same object in memory, while two strings with the same contents will be separate objects. Here is an example of using symbols:
:symbol_name
Strings are used to represent text and can be created with either single or double quotes. For example:
"string"
'string'
[8] How do you raise an exception in Ruby and what is the syntax for doing so?
To raise an exception in Ruby, you use the raise method and pass it the name of the exception class you want to raise. You can also include a message as an argument to provide more information about the exception. Here is an example of raising an exception:
raise ArgumentError, "Invalid argument"
This will raise an ArgumentError exception with the message "Invalid argument".
[9] What is the purpose of the Rails framework and how does it work?
The Rails framework is a web application development framework written in Ruby. It is designed to make it easier to develop web applications by providing a set of conventions and tools for common tasks such as routing, database management, and testing. Rails uses the Model-View-Controller (MVC) architecture to separate the application's data and business logic from the user interface.
[10] How do you implement inheritance in a Ruby class and what is the syntax for doing so?
To implement inheritance in a Ruby class, you use the < operator followed by the name of the parent class. The child class will inherit all of the methods and behavior of the parent class. For example:
class Dog
def initialize(name)
@name = name
end
def greet
puts "Hello, my name is #{@name}!"
end
end
class Puppy < Dog
def wag_tail
puts "Wagging tail!"
end
end
puppy = Puppy.new("Buddy")
puppy.greet # outputs "Hello, my name is Buddy!"
puppy.wag_tail # outputs "Wagging tail!"
In this example, the Puppy class inherits from the Dog class and has access to the greet method, as well as its own wag_tail method.
Top comments (0)