Getting to Know Yourself: Understanding self in Ruby

self is one of those Ruby keywords that looks easy until you meet it in real code. You see it in a setter: self.name = normalized_name Then in a class method: def self.from_cents(...) Then somewhere inside a DSL or instance_eval, and suddenly the question isn't really: What does self mean? The better question is: Who is self right here? That small change makes Ruby's object model much easier to follow. Consider a model-like object: class Customer attr_accessor :name, :email def initialize(name:, email:) @name = name @email = email end def normalize self.name = name.strip.downcase self.email =
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in