Four Key Reasons to Learn Markdown
Back-End Leveling UpWriting documentation is fun—really, really fun. I know some engineers may disagree with me, but as a technical writer, creating quality documentation that will...
Realizing that hitting “talk” on my phone dialed the previous number. Figuring out that the arrow next to my fuel gauge showed which side the car fuel door is on. Tab completion.
A couple of times a month, I’ll have a “wow – that’s so useful and so simple” moment. One of the first times I experienced that with Ruby was with #method_missing
.
Every class in Ruby has a #method_missing
method. When you attempt to call a method that doesn’t exist, #method_missing
will throw a NoMethodError
exception.
You can override this method and do some amazing things with it. At High Groove Studios, we use #method_missing
like a trophy wife uses her rich husband’s credit card bill: without abandon.
For example, let’s say you have a User model, and that user has a ton of profile data. You want to abstract that data, so you make a Profile class.
class User < ActiveRecord::Base has_one :profile end class Profile < ActiveRecord::Base belongs_to :user end
However, you don’t want to call user.profile.street_address
every time you need to access an attribute in a user’s profile. You also don’t want to define a bunch of reader methods like this:
def street_address profile.street_address end
Here’s all you have to do:
class User < ActiveRecord::Base ... # Attempts to pass any missing methods to # the associated +profile+. def method_missing(meth, *args, &block) if profile.respond_to?(meth) profile.send(meth) else super end end end
So, we now have this:
@user.street_address == @user.profile.street_address
That’s only one example of the power and simplicity of #method_missing
.
Writing documentation is fun—really, really fun. I know some engineers may disagree with me, but as a technical writer, creating quality documentation that will...
Humanity has come a long way in its technological journey. We have reached the cusp of an age in which the concepts we have...
Go 1.18 has finally landed, and with it comes its own flavor of generics. In a previous post, we went over the accepted proposal and dove...