Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBembemcha Pebam
2,338 PointsWhy am i able to access the object for the Monster class without passing the 'self' arg?
So in the later part of the tutorial, Jason says we can pass arguments into a block using 'self' to pass in the object itself.
i am still able to access the object without passing the 'self' keyword as an argument.
here is the method: (i have commented out the args passing section)
def scare(&block) actions[:scare] += 1 puts "#{name} scares ..." block.call #self if block_given? # self is passing the class instance to the block end
Now instantiating and calling the various methods work. See below:
m = Monster.new("Gorgon") m.say do #this is the block we are passing in to puts "hello come to the hell" end
m.scream { 5.times do print "huhahahaha!!!!" m.actions[:scream] += 1 end }
m.scare do #|instance| puts "run away!!!!" #puts instance.actions m.say { puts "Hi from scare method call" } #CAN still ACCESS methods end