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 trial
Andrew Stelmach
12,583 PointsHelp me to get my head around this code please!
Really struggling with this, and at the same time I know it's really important I fully understand all of this code before moving onto the next step.
Please could you add to my notes in the code to explain what's going on. Or just write a standard reply if you prefer.
The 'yield' part is the bit that's really stopped me in my tracks, but basically anything that doesn't have a note next to it is something I don't understand.
class Speech
def initialize
print "What is the speech name? " #Ask for the title.
@title = gets.chomp #Get the title
@lines = [] #Make @lines an empty array
while add_line
puts "Line added."
end
end
def title
@title
end
def add_line
puts "Add a line: (blank line to exit)" #Ask for a line.
line = gets.chomp #get the line
if line.length > 0 #if line greater than 0 characters long
@lines.push line #put the line into the @lines array
return line #final value is 'line'
else
return nil
end
end
def each(&block)
@lines.each { |line| yield line}
end
end
speech=Speech.new #Assigns a new instance of the class, 'Speech', to a variable called 'speech'.
speech.each do |var|
puts "[#{speech.title}] #{var}" #Can't access @title directly, but I don't know why exactly. Have to use the title method to access it. Instance variables (which is what it is) are accessible throughout your class, so not outside your class. But why am I able to access the METHOD that's inside that class? Well, I have in my notes that methods are Public by default, so maybe that's why it's accessible from here.
end
4 Answers
Jason Seifer
Treehouse Guest TeacherAndrew Stelmach you got it! Your understanding of yield is right on the money. Regarding why you can't access @title directly and have to use speech.title, it's because of the context of the variable. The @title is an instance variable inside the Speech class. That means that it has to be referenced in an instance of the speed class. When you do speech = Speech.new that speech variable is a new instance of the Speech class, so it can references lines and title.
Andrew Stelmach
12,583 PointsI've been crunching my brain over this a bit more and re-watching the videos. I think this is what happens with the yield part and the called of the each method.
- The each method is called and the first item of the @lines array is assigned to a variable called 'line'.
- Yield is announced, which brings the
puts "[#{speech.title}] #{var}"into the each method....
However, the yield command told ruby to assign the variable 'line' to 'var'. And also, the
puts "[#{speech.title}] #{var}"
is evaluated INSIDE THE METHOD 'EACH'. So, when this line is printed to the screen, 'var' is the first item in the @lines array.
- The method dictates that it runs again for the second item in the @lines array, so 'line' is the 2nd item in the array, that is assigned to the 'var' variable etc etc.
Does that sound fair?
Andrew Stelmach
12,583 PointsNote I changed the name of the 'line' variable to 'var' to make the code easier to understand. One criticism I do have of many of the tutorials is they duplicate names of variables, methods etc, which often actually just makes things harder to understand.
John Steer-Fowler
Courses Plus Student 11,734 PointsWhat error are you getting? Which course step is this for? Need more info please.
Andrew Stelmach
12,583 PointsHi, John. There are no errors, the code runs just fine. It's the code from 'Examples' video in the 'Blocks' badge in the 'Ruby Foundations' course. Hope that's the info you're looking for.
Andrew Stelmach
12,583 PointsBasically, John, I want to have a fluent understanding of this piece of code, and I'm struggling.
Andrew Stelmach
12,583 PointsDoes that sound I've got a reasonable understanding of what's going on? Or am I making a sense of it in a way, but lacking some small but fundamental pieces of understanding?
I think I need to understand this 100% or I'll get tripped up in the future.
Andrew Stelmach
12,583 PointsAndrew Stelmach
12,583 PointsCheers, Jason, that's awesome. I totally understand what's going on now.
And the title method is a neat and simple little trick for making classes' instance variables accessible from outside a class.
Andrew Stelmach
12,583 PointsAndrew Stelmach
12,583 PointsPlease could you take a look at this also? One of the mods has tagged you in it but this is a polite reminder!
I've just spent another couple of hours trying to make it work without using global variables, but it has really has me stumped. https://teamtreehouse.com/forum/dont-understand-whats-asked-in-the-extra-credit