class Foo
def one
p 1
end
def yield_with_self
with self yield
end
def yield_normally
yield
end
end
def one
p "one one"
end
Foo.new.yield_with_self { one } # => 1
Foo.new.yield_normally { one } # => "one"
I really don’t understand that the two ‘one’ methods are defined in the same class as instance methods, what is the difference with ‘with self’ or not when yielding?
If I ‘yield’ without ‘with self’, what is the receiver? Why is the second one not the first one called?
The second one method is defined outside of a class, it’s not an instance method.
Using with self yield is convenient for building nice looking DSLs.
Using with self yield you can imagine this DSL being built:
table :users do
field :name
field :email
end
While without it you’ll need to provide a method receiver self explicitly to achieve similar result and it looks less clean compared to the first example.
table :users do |t|
t.field :name
t.field :email
end
In that case there is no implicit receiver and normal rules apply. In your example the block given to Foo#yield_normally method calls one method which is defined at the top level where your call the block.