>> User.ancestors
=> [User(...), ActiveRecord::Base, ActiveRecord::Aggregations, ActiveRecord::Transactions,
ActiveRecord::Reflection, ActiveRecord::Serialization, ActiveModel::Serializers::Xml,
ActiveModel::Serializers::JSON, ActiveModel::Serialization, ActiveRecord::AutosaveAssociation,
ActiveRecord::NestedAttributes, ActiveModel::SecurePassword, ActiveRecord::IdentityMap,
ActiveRecord::Associations, ActiveRecord::NamedScope, ActiveModel::Validations::Callbacks,
ActiveRecord::Callbacks, ActiveModel::Observing, ActiveRecord::Timestamp,
ActiveModel::MassAssignmentSecurity, ActiveRecord::AttributeMethods::Dirty, ActiveModel::Dirty, ActiveRecord::AttributeMethods::TimeZoneConversion, ActiveRecord::AttributeMethods::PrimaryKey,
ActiveRecord::AttributeMethods::Read, ActiveRecord::AttributeMethods::Write,
| class A | |
| def initialize | |
| @b = B.new | |
| end | |
| def method_missing(m, *a, &b) | |
| @b.public_send(m, *a, &b) # changing this to use send() fixes the error | |
| end |
| class A | |
| def a | |
| b | |
| end | |
| def b | |
| puts "YAY" | |
| end | |
| end |
| ARTICLE_DATA = { 1 => { :status => "published" }, | |
| 2 => { :status => "draft" } } | |
| module BrokenRecord | |
| class Table | |
| def initialize(record_class) | |
| @record_class = record_class | |
| end |
| eval(DATA.read) # :trollface: | |
| ## USE VELLUM TO DESCRIBE A DRAWING IN THE ABSTRACT | |
| drawing = Vellum::Drawing.new(300,400) | |
| drawing.layer(:box) do |g| | |
| g.rect(g.top_left, g.width, g.height) | |
| end |
| require "forwardable" | |
| class BigAssObject | |
| def initialize | |
| @sub_object_1 = Something.new | |
| @sub_object_2 = SomethingElse.new | |
| @sub_object_3 = SomethingEEEELSE.new | |
| end | |
| delegate [:a, :b, :c, :d] => :sub_object_1 |
| class Composite | |
| def initialize(obj) | |
| @target = obj | |
| end | |
| def extend(component) | |
| @target = self.class.new(component.new(@target)) | |
| end | |
| def method_missing(id, *args, &block) |
| require "sqlite3" | |
| SQLite3::Database.new(":memory:") do |db| | |
| rows = db.execute <<-SQL | |
| create table numbers ( | |
| name varchar(30), | |
| val int | |
| ); | |
| SQL |
Inheritance is a key concept in most object-oriented languages, but applying it skillfully can be challenging in practice. Back in 1989, M. Sakkinen wrote a paper called Disciplined inheritance that addresses these problems and offers some useful criteria for working around them. Despite being more than two decades old, this paper is extremely relevant to the modern Ruby programmer.
Sakkinen's central point seems to be that most traditional uses of inheritance lead to poor encapsulation, bloated object contracts, and accidental namespace collisions. He provides two patterns for disciplined inheritance and suggests that by normalizing the way that we model things, we can apply these two patterns to a very wide range of scenarios. He goes on to show that code that conforms to these design rules can easily be modeled as ordinary object composition, exposing a solid alternative to tradi
When Mike Burns outlined his vision of Unobtrusive Ruby, I initially thought it was going to be a hit with the community. However, a lack of specific examples led to a critical backlash and caused the post to generate more heat than light. This is unfortunate, because the ideas he outlined are quite valuable and shouldn't be overlooked.
In this article, I share my own interpretation of what Unobtrusive Ruby means, based on the the points that Mike outlined. I can't guarantee that my take on this is what Mike had in mind, but it should be interesting to those who wanted a better-defined roadmap than what he provided. To get this most out of this article, I recommend going back and reading what Mike wrote before continuing. Try to think about what these concepts mean to you, and then compare them to what I've outlined here.
The following guidelines are the ones Mike laid out, but