
Ruby Metaprogramming
define_method, method_missing, class_eval, instance_eval, hooks, DSLs
1What is metaprogramming in Ruby?
What is metaprogramming in Ruby?
Odpowiedź
Metaprogramming is the ability of a program to write or modify code at runtime. In Ruby, this allows for dynamically defining methods, classes, and modules, making code more flexible and concise. This technique is widely used in frameworks like Rails to create elegant DSLs and reduce code duplication.
2What is the purpose of the define_method method in Ruby?
What is the purpose of the define_method method in Ruby?
Odpowiedź
define_method allows creating methods dynamically at runtime. It takes a method name (symbol or string) and a block defining the method body. Unlike def, define_method captures local variables from the enclosing scope via a closure, which is useful for generating methods with parameterized behaviors.
3When is the method_missing method invoked by Ruby?
When is the method_missing method invoked by Ruby?
Odpowiedź
method_missing is called automatically when an object receives a method call that doesn't exist in its inheritance chain. It receives the name of the called method and its arguments, allowing dynamic handling of these calls. This is the foundation of many metaprogramming patterns like ghost methods used in ActiveRecord.
What is the main difference between class_eval and instance_eval?
Which hook is called when a module is included in a class?
+17 pytań z rozmów
Inne tematy rekrutacyjne Ruby on Rails
Ruby Basics
Ruby Object-Oriented Programming
Rails Fundamentals
Routing & Controllers
ActiveRecord Basics
Views & ERB Templates
ActiveRecord Associations
Advanced ActiveRecord Queries
Rails Forms
Authentication & Authorization
Modern Asset Pipeline & Frontend
Rails API Mode
Testing with RSpec
ActiveJob & Background Jobs
ActionCable & WebSockets
ActionMailer
ActiveStorage
Caching Strategies
Advanced Migrations
Rails Engines & Modular Apps
Performance Optimization
Rails Design Patterns
Rails Security
GraphQL with Rails
Deployment & Production
Monitoring & Logging
Rails Upgrade Strategies
Opanuj Ruby on Rails na następną rozmowę
Uzyskaj dostęp do wszystkich pytań, flashcards, testów technicznych, ćwiczeń code review i symulatorów rozmów.
Zacznij za darmo