Ruby Flashcards
Explain the public access level.
Public Access Modifier:
Public methods are accessible from anywhere, both within the class and from external code.
By default, all methods in a Ruby class are public unless specified otherwise.
Public methods can be invoked on an instance of the class.
Example:
ruby
Copy code
class MyClass
def public_method
puts “This is a public method.”
end
end
obj = MyClass.new
obj.public_method
Explain the private access modifier.
Output:
Private Access Modifier:
those are called withing other methods
Private methods can only be called from within the class itself.
Private methods cannot be accessed from outside the class or by other instances of the class.
Private methods are typically used for internal implementation details and are not meant to be invoked externally.
Example:
ruby
Copy code
class MyClass
def public_method
puts “This is a public method.”
private_method
end
private
def private_method
puts “This is a private method.”
end
end
obj = MyClass.new
obj.public_method
# “This is a public method.”
# “This is a private method.”
obj.private_method # Error: private method `private_method’ called for #<MyClass:0x00007ffcba833ef0> (NoMethodError)
What is the result of a = false || 1
In this expression, the logical OR operator (||) is used. The logical OR operator returns the first truthy value it encounters, or the last value if all operands are falsy.
Explain protected access modifier.
Protected Access Modifier:
Protected methods can be invoked by any instance of the same class or its subclasses.
Protected methods cannot be called from outside the class hierarchy.
Protected methods are typically used to provide controlled access to certain methods within the class hierarchy.
Example:
ruby
Copy code
class MyClass
def public_method
puts “This is a public method.”
protected_method
end
protected
def protected_method
puts “This is a protected method.”
end
end
class Subclass < MyClass
def subclass_method
protected_method
end
end
obj = MyClass.new
obj.public_method
# Output:
# “This is a public method.”
# “This is a protected method.”
subclass = Subclass.new
subclass.subclass_method
# Output: “This is a protected method.”
obj.protected_method # Er
Is Ruby a statically typed or a dynamically typed language?
In a dynamically typed language like Ruby, the type of a variable is determined at runtime, and variables can hold values of any type. The type of a variable can be changed by assigning a value of a different type to it during the execution of the program.
What’s the purpose of the garbage collector module in Ruby?
The purpose of the garbage collector module in Ruby is to automatically manage memory and reclaim memory that is no longer in use. It plays a crucial role in memory management, ensuring that memory resources are efficiently utilized and preventing memory leaks.
Explain hashes
In Ruby, a hash is a collection or data structure that stores key-value pairs. It is also known as an associative array, dictionary, or map in other programming languages. Hashes provide a way to associate data with a unique key, allowing efficient lookup, insertion, and deletion of values based on their associated keys.
Creating a hash using curly braces
student = { “name” => “John”, “age” => 25, “grade” => “A” }
Creating a hash using the Hash.new constructor
car = Hash.new
car[“make”] = “Toyota”
car[“model”] = “Camry”
car[“year”] = 2022
Accessing values in a hash
puts student[“name”] # Output: “John”
puts car[“model”] # Output: “Camry”
Modifying values in a hash
student[“age”] = 26
car[“year”] = 2023
Adding new key-value pairs to a hash
student[“gender”] = “Male”
car[“color”] = “Red”
Removing a key-value pair from a hash
student.delete(“grade”)
Iterating over a hash
student.each do |key, value|
puts “#{key}: #{value}”
end
Explain symbols.
In Ruby, symbols are lightweight, immutable objects that represent names or identifiers. They are often used as keys in hashes, for method names, or to represent a specific value that remains constant throughout the program’s execution.
Here are some key characteristics and benefits of symbols in Ruby:
Immutable: Symbols are immutable, meaning their value cannot be changed once they are created. This immutability makes symbols useful for scenarios where you need a value that won’t be modified accidentally.
Memory Efficiency: Symbols are stored in memory only once, regardless of how many times they are used. This makes them memory-efficient compared to strings, which are duplicated every time they are used. Using symbols can help reduce memory usage in cases where you have many repeated identifiers or keys.
Fast Comparisons: Comparing symbols is faster than comparing strings because symbol comparisons involve comparing object references rather than the actual content of the strings. This speed advantage can be beneficial when symbols are used for key lookups in hashes or when checking equality.
Common Usage in Hashes: Symbols are commonly used as keys in hashes because they provide efficient and meaningful identifiers for accessing values. Since symbols are unique and immutable, they can serve as reliable keys for hash-based lookups.
As hash keys
person = {
name: “John”,
age: 30,
occupation: :engineer
}
puts person[:name] # Output: “John”
puts person[:occupation] # Output: :engineer
As method names
class MyClass
def my_method
puts “Hello from my_method”
end
end
obj = MyClass.new
obj.send(:my_method) # Output: “Hello from my_method”
As constants or flags
STATUS_SUCCESS = :success
STATUS_FAILURE = :failure
puts STATUS_SUCCESS # Output: :success
puts STATUS_FAILURE # Output: :failure