define_method - Metaprogramming in Ruby

preview_player
Показать описание
In this edition, we talk about the ruby metaprogramming tool: define_method for dynamically defining methods on objects. define_method allows you to write very generic classes that automatically add functionality on the fly. Follow along to become even better with metaprogramming in ruby.

Presenter:

Рекомендации по теме
Комментарии
Автор

Great video, this was exactly what I was looking for!

Sarahchamorro
Автор

Thank for sharing, Great Video for Ruby Define Method

elmondo.
Автор

Super interesting 😎 I'm new to this side of ruby and its pretty cool

jacobdegeling
Автор

Really helpful video, thanks a lot !!0

nandodevia
Автор

Great thanks, i could'n get how to make this even looking to source of ActiveRecord validate methods.
This seems 100.times easier than that and than i though it should be)

sergeIwakura
Автор

Instead of clear command you can use Ctrl+L shortcut

sergeIwakura
Автор

Hi! Unfortunately, this won't work on multiple classes. If you will define another class, let's say, Post, and give it an enum "status" with values [:draft, :published, :archived], you won't be able to specify two separate enums on the same key. What is happening here is that you define the method dynamically, yes, but on the ModelBase class globally. To achieve what you're trying to do, and what Rails does, you need to approach it with the eigenclass, which will be unique for both Event and Post classes that inherit from the model class.

Good stuff, though :) Cheers!

BiGBoB