For example a Person class dealing with a bunch of forenames:
class BankAccount attr_accessor :attributes def initialize attributes={} @attributes = attributes.assert_valid_keys :name, :amount end def add_money money @attributes[:amount] += money end def attribute attribute_name @attributes[attribute_name] end def attribute= attribute_name, value @attributes[attribute_name] = value end endand:
bank_account = BankAccount.new name: 'John', amount: 1000 bank_account.add_money 200 bank_account.attribute :amount => 1200 bank_account.attribute :amount, 2000 bank_account.attribute :amount => 2000can be refactored to:
class BankAccount attr_accessor :attributes def initialize attributes={} @attributes = attributes.assert_valid_keys :name, :amount end def << money @attributes[:amount] += money end def [] attribute_name @attributes[attribute_name] end def []= attribute_name, value @attributes[attribute_name] = value end endSending the messages is not only less typing but more important it is way more clear what happens:
bank_account = BankAccount.new name: 'John', amount: 1000 bank_account << 200 bank_account[:amount] => 1200 bank_account[:amount] = 2000 bank_account[:amount] => 2000
Supported by Ruby 2.1.1 and Ruby on Rails 4.1.8