My day job is designing and developing websites, but in my off time, I like to build all kinds of things, including Ruby gems, iPhone apps, and Alexa skills.
Putting aside the use of curly braces on a multi-line block, two things really stand out to me about this:
I'm looping over an array to add the elements to another array when I could easily just set @playlist to the songs array.
Not only am I looping over the elements in initialize, but I'm calling a second method that also loops over an array with each individual element before adding them to the @playlist array.
Thankfully someone on Stack was able to show me the light and help me realize I could easily refactor the class to the following:
For what it's worth, there is some merit in making a copy of the list, if you don't want to mutate the list that's been passed in at a later time for some reason. I'm not versed in ruby and I assume it does have a list copy operator of some sort, but there's at least merit in what you were doing, if not the mechanism. :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's mine. It's from way back in October, 2014.
What really stood out to me about this wasn't so much the question itself, but the code I wrote for the Playlist class:
Putting aside the use of curly braces on a multi-line block, two things really stand out to me about this:
Thankfully someone on Stack was able to show me the light and help me realize I could easily refactor the class to the following:
Totally could see my past jr dev self doing this!
For what it's worth, there is some merit in making a copy of the list, if you don't want to mutate the list that's been passed in at a later time for some reason. I'm not versed in ruby and I assume it does have a list copy operator of some sort, but there's at least merit in what you were doing, if not the mechanism. :)