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 trialWilliam Bode
7,105 PointsUseful Applications of Conditional Assignment?
Hey,
can someone give me one or better two examples, when conditional assignment is useful?
I understand the technique, but I am not sure, when in "real programming" I need to remember this concept.
Thanks a lot!
2 Answers
Raymond Sapida
33,049 PointsHi there,
I see the conditional assignment applied in Rails methods like current_user
as an optimization. It prevents the program from assigning it every time it's called. This link gave me a good explanation for it.
I've also read it as Nil Guards in the Metaprogramming Ruby book. It's useful for cases where you don't want to return nil
since it allows you to assign an empty string or array if it's not defined yet
I hope this was helpful!
William Bode
7,105 PointsThank you Raymond, this is exactly the answer I wanted.