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

Ruby

This part of the shopping_list.rb program honestly seems repetitive - there must be a way to minimize it...

I reviewed the shopping_list.rb program and realized that this argument seems incredibly repetitive - especially since we made a method to minimize the separator code.

list['items'].push(add_list_item()) list['items'].push(add_list_item()) list['items'].push(add_list_item())

Is there no way to convert this into a more concise method? It just seems really glaring.

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, there, I'm not sure the code you posted come from which Ruby course or lecture.

list['items'].push(add_list_item())
list['items'].push(add_list_item())
list['items'].push(add_list_item())

You're asking if there's a way to DRY up this piece of code, right? Of course, for example.

3.times do
  list['items'].push(add_list_item())
end

This is just one way, I'm sure there're other ways to minimize the repetitive codes. Ruby, as a programming language, is well-known for its elegant & straightforward syntax that makes writing clean code very easy.