Metaprogramming in Ruby allows you to write code that manipulates code itself. These questions will help you understand defining methods dynamically, introspection, reflection, and designing DSLs in Ruby.
Topic 1: Defining Methods Dynamically
Which Ruby method allows you to define methods dynamically? a) define_method() b) method() c) create_method() d) define_dynamic_method()
How do you dynamically create a method in a Ruby class? a) class.method(:method_name) b) class.define_method(:method_name) c) class.add_method(:method_name) d) class.create_method(:method_name)
Which of the following is the correct way to define a method dynamically using define_method? a) define_method(:new_method) { puts "Hello, world!" } b) method(:new_method) { puts "Hello, world!" } c) create_method(:new_method) { puts "Hello, world!" } d) add_method(:new_method) { puts "Hello, world!" }
When defining methods dynamically, which block can access the receiver of the method? a) self b) block c) yield d) super
How do you dynamically define a method with arguments in Ruby? a) define_method(:method_name) { |arg| puts arg } b) method(:method_name) { |arg| puts arg } c) create_method(:method_name) { |arg| puts arg } d) define_method(:method_name, arg) { puts arg }
What happens when you try to define a method dynamically using an undefined symbol? a) An error is raised b) It silently ignores the method c) It defaults to a no-op method d) It reverts to a default method
How can you call a dynamically defined method in Ruby? a) send(:method_name) b) call(:method_name) c) invoke(:method_name) d) run(:method_name)
What is the primary use of dynamically defined methods in Ruby? a) To reduce code repetition b) To increase performance c) To create private methods d) To make classes immutable
Which of the following can be done with define_method? a) Define private methods only b) Define methods with varying arguments c) Create new classes dynamically d) Define instance variables dynamically
What is the purpose of send when calling a dynamically defined method? a) To execute the method as an object message b) To call a private method c) To bind a method to a specific class d) To define the method dynamically
Topic 2: Introspection and Reflection
Which method in Ruby helps you check the class of an object? a) object.class b) object.inspect c) object.type d) object.to_class
What method returns the list of instance variables for an object in Ruby? a) object.variables b) object.instance_variables c) object.variables_list d) object.instance_vars
Which method is used to retrieve the list of methods defined for an object? a) object.methods b) object.method_list c) object.get_methods d) object.methods_defined
What is the purpose of respond_to? in Ruby? a) To check if an object responds to a method b) To define a method for an object c) To find the class of an object d) To get the methods available to a class
How do you determine if an object has a specific instance variable? a) object.has_instance_variable?(:var_name) b) object.has_variable?(:var_name) c) object.instance_variable_defined?(:var_name) d) object.variable_defined?(:var_name)
Which of the following methods can be used to inspect an object in Ruby? a) object.inspect b) object.show c) object.inspect_method d) object.display
What method would you use to get the list of all instance methods of a class? a) class.instance_methods b) class.methods c) class.get_methods d) class.methods_list
Which method allows you to invoke a method by its name as a symbol? a) call_method(:method_name) b) send(:method_name) c) invoke(:method_name) d) call(:method_name)
Which method is used to check if a method exists for an object in Ruby? a) object.method_defined?(:method_name) b) object.has_method?(:method_name) c) object.respond_to?(:method_name) d) object.method_exist?(:method_name)
What is the function of instance_variable_get? a) It retrieves the value of an instance variable b) It sets the value of an instance variable c) It checks if an instance variable exists d) It defines a new instance variable
Topic 3: DSL (Domain-Specific Language) Design
What is a Domain-Specific Language (DSL)? a) A language designed for general-purpose programming b) A language specialized in a specific problem domain c) A language that does not need to be compiled d) A library used for web development
Which of the following is an advantage of using a DSL in Ruby? a) It reduces the amount of boilerplate code b) It improves execution speed c) It reduces memory usage d) It makes the language more secure
In Ruby, what method can be used to implement a block of code within a DSL? a) yield b) block_call c) invoke_block d) def_block
Which of the following is the primary goal of a DSL? a) To extend the functionality of Ruby b) To make the code more readable and expressive c) To reduce the need for external libraries d) To enhance the performance of Ruby applications
What type of Ruby construct is commonly used in DSLs to make code more expressive? a) Blocks b) Arrays c) Hashes d) Classes
How do you create a simple Ruby DSL for configuration purposes? a) By defining methods and using blocks b) By creating complex classes c) By using a gem like rails d) By modifying the Ruby syntax
In Ruby DSLs, what does the self keyword typically refer to within a block? a) The object that the DSL is being applied to b) The Ruby interpreter c) The main program d) The method being called
What is a key feature of Ruby that makes it ideal for creating DSLs? a) Dynamic typing b) Static typing c) Compilation d) Memory management
Which of the following Ruby methods is commonly used to define an internal DSL? a) define_method b) self.method c) create_dsl d) set_dsl
How do DSLs improve the usability of a program? a) By allowing a developer to write less code b) By making the code more readable and specific to a domain c) By reducing the program’s memory usage d) By improving the performance of algorithms
Answers Table
Qno
Answer (Option with the text)
1
a) define_method()
2
b) class.define_method(:method_name)
3
a) define_method(:new_method) { puts "Hello, world!" }
4
a) self
5
a) `define_method() {
6
a) An error is raised
7
a) send(:method_name)
8
a) To reduce code repetition
9
b) Define methods with varying arguments
10
a) To execute the method as an object message
11
a) object.class
12
b) object.instance_variables
13
a) object.methods
14
a) To check if an object responds to a method
15
c) object.instance_variable_defined?(:var_name)
16
a) object.inspect
17
a) class.instance_methods
18
b) send(:method_name)
19
c) object.respond_to?(:method_name)
20
a) It retrieves the value of an instance variable
21
b) A language specialized in a specific problem domain
22
a) It reduces the amount of boilerplate code
23
a) yield
24
b) To make the code more readable and expressive
25
a) Blocks
26
a) By defining methods and using blocks
27
a) The object that the DSL is being applied to
28
a) Dynamic typing
29
a) define_method
30
b) By making the code more readable and specific to a domain