Ruby: Accessing fields
I’ve spent a little time browsing through some of the libraries used by my project and one thing which I noticed in ActiveSupport is that fields don’t seem to be accessed directly but rather are accessed through a method which effectively encapsulates them inside the object.
For example the following function is defined in 'inheritable_attributes.rb'
def write_inheritable_attribute(key, value)
if inheritable_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES)
@inheritable_attributes = {}
end
inheritable_attributes[key] = value
end
def inheritable_attributes
@inheritable_attributes ||= EMPTY_INHERITABLE_ATTRIBUTES
end
EMPTY_INHERITABLE_ATTRIBUTES = {}.freeze unless const_defined?(:EMPTY_INHERITABLE_ATTRIBUTES)
If we were using C# we’d have instantiated '@inheritable_attributes' at the field definition with 'EMPTY_INHERITABLE_ATTRIBUTES' like so…
public class SomeClass {
private Dictionary<string, object> inheritableAttributes = new Dictionary<string, object>();
}
…but we can’t do that in Ruby because we don’t need to explicitly define all our fields, we just start using them.
I’m assuming this is quite a common pattern in Ruby and in a way it’s quite neat because it restricts the number of direct field references which will make it easier to change the underlying implementation. Kerievsky’s narrowed change refactoring suddenly becomes less necessary!
For that reason I wonder whether it would be a useful pattern in C#/Java or if it would be overkill.
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.