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 trialtako izu
17,645 Pointsa block argument really isn't necessary here
i watched this video and all the way through i was thinking to myself: "can't this be done with one single method, rather than a method and a block argument?"
this is what the finalised code from this video was:
def get_name
print "Enter your name: "
name = gets.chomp
yield name
name
end
my_name = get_name do |name|
puts "That's a cool name, #{name}!"
end
puts "my_name: #{my_name}"
this is what it says in the console:
- Enter your name:
tako
- That's a cool name, tako!
- my_name: tako
twelve lines: that seemed a bit excessive for something as simple as returning a name variable. it goes through the method, asking the user their name, then it sets the input to the variable name
, then yields the block argument and returns the variable name
. the block argument basically just prints that's a cool name, #{name}!
without much else going on. finally, it prints my_name: #{my_name}
. a lot of work. looking for something simpler than this, i iterated the code and i ended up with this:
def get_name
print "Enter your name: "
name = gets.chomp
puts "That's a cool name, #{name}!"
return name
end
my_name = get_name()
puts "my_name: #{my_name}"
this is what it says in the console:
- Enter your name:
tako
- That's a cool name, tako!
- my_name: tako
this is only nine lines. i will admit, this isn't much of a difference, but it's a lot better than having to go through a method and a block argument. it simply asks the user for their name, sets that input to the name
variable, prints that's a cool name, #{name}!
then returns name
. it sets the returned value to my_name
and then prints it. one method for one task!
correct me if i'm wrong, but so far i haven't learned much about the exclusive abilities that block arguments can do. maybe i might learn it in the future, however right now i just don't see a reason to use it.
alsoo sorry for ranting i am just really confused and a bit angry
1 Answer
Steven Parker
231,236 PointsI have not taken this course, but from my experience with numerous other courses here I have learned that the examples are rarely ever the best and most efficient solution to a programming task. Instead, they generally focus on showing how to apply a concept or technique relevant to the current lesson or stage.
Once I realized this, it made sense to me because the most efficient solution might not be the one that best exemplifies the lesson topic for a student.
I consider noticing a better, more compact, or more efficient way to code the task simply evidence of your learning progress and mastery of the overall concepts. Good job!