Open ThomasBush opened 10 years ago
Others have probably written about it better than I can. Does this help at all? http://stackoverflow.com/questions/7848020/rails-newbie-about-yield/7848137#7848137
And perhaps looking at a specific confusing case might make it easier to explain.
Jason Swett Principal, Ben Franklin Labs Cell: (616) 856-8075
On Jul 18, 2014, at 10:53 AM, ThomasBush notifications@github.com wrote:
Hey all,
I am working through the Ruby Bits tutorials at CodeSchool. I am completing the section about yields. In the challenges we yield, yield objects, and yield block(I think?) to other blocks and while I can complete the code challenges I don't quite understand what (or why) I am yielding. I was hoping someone could dumb down this concept of a Yield a little so I could have a more complete understanding.
I think I get the idea when looking at the rails application layout. We do some header stuff, yield to the specific template, than do some footer stuff. I think where I get confused is yielding blocks or objects to other blocks or objects.
I would really appreciate if someone could shed some light on this concept for me.
Thanks! Thomas Bush
— Reply to this email directly or view it on GitHub.
@ThomasBush What section are you on? Is it Ruby Bits 1 or 2?
@atkolkma I am on Ruby Bits 1 section 6.
@jasonswett Thanks for the quick response, I am more looking for pure ruby yield, I now realize my example would lead you to believe otherwise. I assume the named yields are rails implementation. It is tough for me to ask as I feel like I don't understand enough to ask the right questions in this situation. My the exact challenge would be easier to present.
"Now that our library is complete, let's play some games! A friend has given us his Emulator class to use, and we've implemented methods to play a game and grab a screenshot. But look at all that duplicated code in play and screenshot. Refactor the duplication (the begin, new and rescue parts) into a private method called emulate that handles the emulator setup and exception handling and yields the emulator instance to a block."
Given Emulator.rb and Game.rb before and after refactor
Getting from the before to the after confuses me, specifically the part where methods play and screen shot have blocks added. I understand we want to pull out duplicate code so the formation of the emulate method makes sense to me. The implementation of this and the other methods mentioned above is where I get lost.
Sorry, just realized I labeled the gist before and after refractor files incorrectly. Flipped the names. should be good now.
Okay, I’ll do my best to explain it. First, check this out: http://www.tutorialspoint.com/ruby/ruby_blocks.htm (maybe you have already)
Look specifically at this example:
def test yield 5 puts "You are in the method test" yield 100 end test {|i| puts "You are in the block #{i}"} That second part could also be written as:
test do |i| puts “You are in the block #{i}” end
That looks kind of like
emulate do |emulator|
emulator.play(self)
end
So what’s happening (I believe) is when you do “yield emulator”, you’re plugging the value of Emulator.new(system) into your block, similar to how you’re plugging 5 and 100 into the “test {|i|…” block in the example.
I sure hope I explained that correctly. I would look at the code from the page I linked and just try to slowly swap out its variable names for yours until it’s clear where the similarities are.
Does that help?
Jason Swett Principal, Ben Franklin Labs Cell: (616) 856-8075
On Jul 18, 2014, at 1:08 PM, ThomasBush notifications@github.com wrote:
Sorry, just realized I labeled the gist before and after refractor files incorrectly. Flipped the names. should be good now.
— Reply to this email directly or view it on GitHub.
@ThomasBush
This was really confusing to me at first, and sometimes still is.
You can DRY up your code by abstracting out the initialization that appears in several methods. emulate
now does all the instantiation, and yield
allows you to pass in arbitrary blocks into emulator. emulate
basically says:
1) Create a new instantiation of Emulator
2) Do whatever the method calling me says to do, via the block that's passed into me (executed with yield
)
3) Rescue any errors that might occur from executing
Just imagine dropping the whole block, wherever it is called in other methods, into the emulate
method.
Hopefully I didn't butcher that
Thanks guys, the combination of these answers help me wrap my head around this. I really appreciate the extra effort.
Hey all,
I am working through the Ruby Bits tutorials at CodeSchool. I am completing the section about yields. In the challenges we yield, yield objects, and yield blocks (I think?) to other blocks and while I can complete the code challenges I don't quite understand what (or why) I am yielding. I was hoping someone could dumb down this concept of a Yield a little so I could have a more complete understanding.
I think I get the idea when looking at the rails application layout. We do some header stuff, yield to the specific template, than do some footer stuff. I think where I get confused is yielding blocks or objects to other blocks or objects.
I would really appreciate if someone could shed some light on this concept for me.
Thanks! Thomas Bush