-
Notifications
You must be signed in to change notification settings - Fork 34
Open
Labels
2.0Feature request for 2.0Feature request for 2.0
Milestone
Description
Hi there!
First, thanks a lot for your work! 💚
Problematic Script
require "docile"
class Parent
attr_reader :children
def initialize
@children = []
end
def add_child(clazz, &block)
child = clazz.new
children << child
Docile.dsl_eval(child, &block) if block
child
end
end
class CustomParent < Parent
def do_stuff
add_child Parent do
wanna_be_custom
end
end
def wanna_be_custom
add_child CustomParent
end
end
parent = CustomParent.new
parent.do_stuff
p parent.childrenActual Output
Both Children are added to the main parent/within the block self isn't shifted to the child
[#<Parent:0x000055794759c598 @children=[]>, #<CustomParent:0x00005579475ab7a0 @children=[]>]
Expected Output
the main parent has one child, which itself then has the CustomParent as a child. (within the block self correctly is the child)
[#<Parent:0x000055d997c50460 @children=[#<CustomParent:0x000055d997c5b658 @children=[]>]>]
This output can currently be achieved in 2 ways:
- inline
wanna_be_custom
def do_stuff
add_child Parent do
add_child CustomParent
end
end- in
add_child, instead of instantiating the passed in class just useself.class(yes this changes semantics but might give a hint as to the source of this)
def add_child(clazz, &block)
child = self.class.new
children << child
Docile.dsl_eval(child, &block) if block
child
endMight be related to #31 but it's marked fixed 🤷♂️
edit: After a good night sleep I finally realized that of course this is about self being the wrong thing so much like #31 ;)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
2.0Feature request for 2.0Feature request for 2.0