5 ms·
class Blog < ActiveRecord::Base end Ever wonder how that works? Metaprogramming. enum status: [ :active, :archived ]. validates :status, presence: true
by asattarmd 12y ago
class Blog < ActiveRecord::Base
end
Ever wonder how that works? Metaprogramming.
enum status: [ :active, :archived ].
validates :status, presence: true
Ever wonder how that works? Metaprogramming.
Used the awesome debugger, pry? Ever wonder how that works? Metaprogramming.
`find_by_id` isn't the only thing that's implemented using Metaprogramming. Practically, everything within ActiveModel is achieved through Metaprogramming. It is one very important reason that allows Ruby to be so expressive.
- weaksauce 12y agoAlso, metaprogramming allows one to create find_by_id as a function at runtime after the first use of it, it becomes part of the class.
- MichaelGG 12y agoYou can expose the same functionality in, say, C# or F# if you want to give up static type checking. (Actually with F# type providers or a language with good macros, you can keep static analysis if you're willing to bind yourself to the database at build time.) This is just a use of dynamic binding. That's fine, I just thought there was more to it, something that requires a lack of static analysis, multi threading, etc.