<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Brittany</title>
    <description>The latest articles on DEV Community by Brittany (@brittanygrebnova).</description>
    <link>https://dev.to/brittanygrebnova</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F105421%2F026a8336-9139-4fd7-8aa2-8c8c72b5271d.jpeg</url>
      <title>DEV Community: Brittany</title>
      <link>https://dev.to/brittanygrebnova</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brittanygrebnova"/>
    <language>en</language>
    <item>
      <title>Music Libary CLI</title>
      <dc:creator>Brittany</dc:creator>
      <pubDate>Fri, 19 Oct 2018 12:02:40 +0000</pubDate>
      <link>https://dev.to/brittanygrebnova/music-libary-cli-2elj</link>
      <guid>https://dev.to/brittanygrebnova/music-libary-cli-2elj</guid>
      <description>&lt;p&gt;It came. It saw. It conquered my entire life for a whole week.&lt;/p&gt;

&lt;p&gt;The Music Library CLI was the first of the final projects for Object Oriented Ruby. To say that it was challenging is an understatement; it truly tested me on all the knowledge I had gained in the preceding lessons. In this blog post, I would like to iterate over ; ) the final method that brought everything together - #play_song.&lt;/p&gt;

&lt;p&gt;The first line:&lt;/p&gt;

&lt;p&gt;def play_song puts “Which song number would you like to play?”&lt;/p&gt;

&lt;p&gt;The CLI asks the user which song they would like to play from the imported library, a simple puts statement. The CLI then ‘gets’ the user’s response in the next line:&lt;/p&gt;

&lt;p&gt;user = gets.strip&lt;/p&gt;

&lt;p&gt;Then the program checks to make sure that the number entered by the user is a valid number corresponding to one of the song options in the music library. This is accomplished by the following code:&lt;/p&gt;

&lt;p&gt;if user.to_i.between?(1, Song.all.length)&lt;/p&gt;

&lt;p&gt;Song.all.length is the number of items in the Song.all array. If the number entered is valid, the CLI then matches the number entered with the corresponding item in the Song.all array through this line of code:&lt;/p&gt;

&lt;p&gt;song = Song.sorted[user.to_i - 1]&lt;/p&gt;

&lt;p&gt;The song variable is assigned to the value of the sorted class method, which looks like this:&lt;/p&gt;

&lt;p&gt;def sorted &lt;br&gt;
    self.all.uniq.sort_by {|s| s.name} &lt;br&gt;
end&lt;/p&gt;

&lt;p&gt;The return value of this method is a uniqe array of items listed alphabetically. In the case of Song.sorted, it is an array of songs listed in alphabetical order. So calling Song.sorted[user.to_i - 1] takes the user’s input, subtracts 1 from the number in order to correspond to the array items that start with a 0 index, and chooses the song at that index from the Song.all array. The next line:&lt;/p&gt;

&lt;p&gt;puts “Playing #{song.name} by #{song.artist.name}”&lt;/p&gt;

&lt;p&gt;Here, the program uses string interpolation to return the .name and .artist.name of the matching song item in the Song.all array.&lt;/p&gt;

&lt;p&gt;It was interesting to incorporate the use of modules in this lab, which was a concept recently introduced in the course. I really like the sleekness of having a single file with a body of methods that can be applied to each class.&lt;/p&gt;

&lt;p&gt;Here’s the final #play_song method:&lt;/p&gt;

&lt;p&gt;def play_song&lt;br&gt;
  puts "Which song number would you like to play?"&lt;br&gt;
  user = gets.strip&lt;br&gt;
    if user.to_i.between?(1, Song.all.length)&lt;br&gt;
      song = Song.sorted[user.to_i - 1]&lt;br&gt;
      puts "Playing #{song.name} by #{song.artist.name}"&lt;br&gt;
    end&lt;br&gt;
end&lt;/p&gt;

&lt;p&gt;Thank you for reading : )&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>cli</category>
      <category>womenwhocode</category>
      <category>codingmoms</category>
    </item>
    <item>
      <title>Object Oriented Ruby</title>
      <dc:creator>Brittany</dc:creator>
      <pubDate>Fri, 19 Oct 2018 03:53:48 +0000</pubDate>
      <link>https://dev.to/brittanygrebnova/object-oriented-ruby-18jo</link>
      <guid>https://dev.to/brittanygrebnova/object-oriented-ruby-18jo</guid>
      <description>&lt;p&gt;The introduction of object orientation in Ruby seems to be well-timed and intuitive within this course. The ways that we learned to build methods before are now making sense in the context of a class. Before learning about object orientation, I kept wondering where all these methods were going to be used, how they tied together, and what was their ultimate purpose. Now that object orientation has become the name of the game, it’s clear that we use these methods to bring objects to life.&lt;/p&gt;

&lt;p&gt;One of the most recent ‘Aha!” moments that I’ve had was related to the reader and writer methods that are involved in building a class. When OO was first introduced, and the process of defining the #name= and #name methods was being taught, I felt like I was mechanically typing the same thing over and over without comprehending what was happening. Why did we need two different methods to represent the same variable? And what does that = sign do? I didn’t get it.&lt;/p&gt;

&lt;p&gt;Then, somewhere along the way, I realized that when we call name= (or any variable=) in our program, we are setting that variable equal to a value for a particular instance of a class. We are writing that variable’s value. We also need an additional method to recognize that value and allow it to be referenced and operated on with all the capabilities that the class has to offer, hence the necessity for the reader method that includes the @variable, the instance variable. The instance variable is recognized throughout the scope of the class.&lt;/p&gt;

&lt;p&gt;I finally wrapped my mind around this concept when along came attr_accessor to replace these two methods! Well, not always, but in many cases. Writing out the reader and writer methods is the foundation, and using attr_accessor is the next level of abstraction that streamlines our code and makes our lives easier. I get it!&lt;/p&gt;

&lt;p&gt;Thank you for reading : )&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>objectorientedruby</category>
      <category>codingmoms</category>
    </item>
  </channel>
</rss>
