Metaprogramming

Contents
Send messages
define_method
method_missing
Freeze shared state

Send messages

Every method call is a message, and send delivers a message chosen at runtime.

Pass the method name as a symbol or a string, and prefer public_send when private methods must stay off limits.

name = "ada" puts name.send(:upcase) puts name.public_send("upcase")

ruby send_demo.rb

ADA
ADA

define_method

The define_method call creates methods inside a loop, so similar methods need no repetition.

Each generated method closes over its own word, so hello and bye remember different values.

class Greeter ["hello", "bye"].each do |word| define_method(word) do puts word.upcase end end end Greeter.new.hello

ruby greeter_demo.rb

HELLO

method_missing

When no method matches, Ruby calls method_missing instead of failing right away.

Read the value from the hash when the key exists, call super otherwise, and mirror the logic in respond_to_missing?

class Config def initialize(values) @values = values end def method_missing(name, *args) return @values[name] if @values.key?(name) super end def respond_to_missing?(name, include_private = false) @values.key?(name) || super end end config = Config.new({ theme: "dark" }) puts config.theme

ruby config_demo.rb

dark

Freeze shared state

Shared constants invite accidental changes, and freeze locks an object against mutation.

A frozen hash raises FrozenError on write, while frozen? reports the locked state.

SETTINGS = { theme: "dark", retries: 3 }.freeze puts SETTINGS.frozen?

ruby freeze_demo.rb

true

Next: Enumerators

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: