Monday, September 18, 2017

Method Madness

While learning to code, enumerable methods have proven themselves time and again to be invaluable. Often I will find myself writing 5-6 lines of code for a method and realizing it could have all been completed by a simple, one or two word method. Im going to explore how some of them that might not be as well known, at least I haven't used them often yet. 

.all?
This enumerable method takes a block and for each object passed in, assesses if it is true or false. If all the objects are true the return value will be true. If even one of the objects returns false or nil, the method will return false. If no block is given .all? provides one that would read {|object| object} so that it can look at each element in the array or hash being acted upon.

given the following code:

Here we see that the arrays that have any odd number in them return false, but the array with only evens included returns true!


.each_with_object

the .each_with_object enumerable method is a bit complicated to understand and the ruby documentation includes only a short description of what this elusive method can do. 
If you want to solve a problem like the one below in which we would like to return a hash where the key is the original object and the value is the new object after being acted on by the block.

.each_with_object offers a way to do this without sandwiching 


The tricky bit for this method is that it returns the original object given, just as .each does. 

I believe what this means is that the original value is not altered by the method, it stays the same, but is used to create a new object based on the original and the block. The output can include both of these objects.


.take / .take_while
These two methods are somewhat specific, but can certainly be useful to know of!  The .take method is given an argument of a fixnum and then will take that number of elements from the start of the enumerable that the method is called on.

The .take_while method takes a block and evaluates if the return value is true or false. Once it hits a false value it stops iterating over the element and will return all the elements from the enumerator that came before the false.


Tuesday, September 5, 2017

Youtube from scratch


I have been really struggling with learning to manipulate Object Relationships in Ruby.  Wrapping my mind around the logic that follows from one class to another remains tricky through as many number of times as I have practiced it. Still, I know it is super important and will be used throughout the rest of the course at Flatiron School and beyond. So I have continued to find new ways of practicing building classes and mapping these relationships. This week I decided to create my own project by modeling the domain of Youtube. 

The website used by millions of people to replace the void left in our lives after Americas Funniest Home Videos went off the air. 



I began making the model using the three classes: Poster (someone who makes a video and posts it), Viewer, and Video. I initialized the Poster and Viewer with usernames and the Video with a title.  I knew the Video would need to know who posted it and be able to find out who was watching it, so I added poster and viewer to the Video class. Then I could begin writing methods!

I wrote the basics, a method to collect all the Viewers, all the Videos, all the Posters.  A method that allows the Poster to post a video and allows them to know who the viewers are of their videos. 
But once I began writing a method that would tell which videos a user has watched, and then one to count how many times a user has watched each video (cause we all know we have those videos we can watch over and over...)

When trying to write these methods however, it seemed like something was missing. I just couldn't seem to get them to work with the setup how it was. so I re-examined my model and found that I was indeed missing something that could make everything else fit together. Instead of just using the Video class as the through class, I needed to add a Watch class to create instances of a viewer watching a video and collect those instances to be counted and examined in other class' methods. Once I created this fourth class I had a model unlike anything I'd worked with before.  Could I really have a has-many-has-many-through model? Would the poster still be able to understand who the viewers are though that extra layer? 



Its totally possible!



By using the Video and Watch classes both as through classes I was able to write methods that could relate to them all. The Poster was able to collect the usernames of the viewers.


The Video class has methods that can count the number of views for each video and collect the usernames of the viewers.


And the Viewer class is able to collect a list of all the watched videos. Produce a count of how many times each video has been watched and then output the viewer's favorite video based on the video that has been watched the most. 



The hardest part of this model was writing these last three methods. all_watched_videos required looping through an instance twice in order to return only each video's title. The watch_count method required creating a new hash and placing each video into key-value pairs within that hash and then incrementing a key's value each time another instance of the same video was encountered in the all_watched_videos array. Then favorite_video required iterating over the hash from watch_count and putting the title, found in the key of the hash, which has the highest value.
If I were to continue working on this, I would like to add a comments class, and add methods where a viewer could leave a comment, and a Poster could respond to a comment.

This project may have become bigger and trickier than I expected, but it taught me a lot about object relationships. 


Thursday, August 17, 2017

Coding with Friends

Working on the pre-work for Flatiron School was a lonely experience. Working from home to begin a learning a new skill was something I had done many times before. However this time I felt like I had so many more questions and frustrations with the process. Sure, I could ask questions through the Learn website. This was sometimes helpful and also began to connect me with my classmates, but it was slightly intimidating to do so. I needed to sort out what those questions even were, then how to word them so that someone else would understand. I think I ended up only asking 2 questions through the entire process.


All this changed once we got into the classroom. That is when I discovered the power learning together can have. For even the smallest question or confusion I could suddenly turn to the person  next to me and say "do you get this?' Every time, this was met with either "Yes, here's what you're not seeing" or "No, lets figure it out together." The learning process instatnly became collaborative. 


"Students learn a great deal by explaining their ideas to others and by participating in activities in which they can learn from their peers. They develop skills in organizing and planning learning activities, working collaboratively with others, giving and receiving feedback and evaluating their own learning." - From - Peer Learning in Higher Education: Learning From & With Each Other

There have been several studies evaluating the effectiveness of learning with others who are at the same level, as well as at varying levels of learning. Each style was found to be valuable in its own way. At Flatiron school I have also found this to be true. When approaching a new subject along side someone who is also unfamiliar with it, we each see the information in different ways and then work together to understand how both the perspectives are valid. When working with someone who is a peer but has already mastered the subject I can trust that their perspective on the subject works to provide the desired results. 

"The results of this study also suggest that peer-assisted learning increases confidence; reduce anxiety and stress and effect on development of learner’s future responsibilities." - From - Evaluation of the efficacy of peer-learning method in nutrition students of Shiraz University of Medical Sciences
A phrase I have heard many times since classes began is "by teaching you, I solidified the concept for myself." This is a technique used often in education. By teaching others, you teach yourself. Often when working to find a way of demonstrating or explaining an concept, just the act of figuring out how to do that will force you to re-examine the material and you gain a deeper understanding of it. 

So we know that peer learning works. However, we cant all be so lucky as I am right now to be surrounded by others learning with me. What do we do when that happens? how can I learn anything once school is over? Or if I'm working on material at home alone?  

Thats were the magic of Rubber Duck Debugging comes in!


Sometimes, I will ask someone a question and just in the process of asking and explaining my problem I discover the solution. The Rubber Duck method is based on this. A programmer will explain the code and an issue, out loud, to a duck. By explaining what the code is supposed to do and then observing what it actually does, the divide can be seen more clearly and the problem is found. Ultimately they are just explaining the issue back to themselves, but it works! 


 Sometimes your mind can become so enveloped in the code that you cant see the big picture. The Rubber Duck method helps you to pull back the perspective.



By combining these two methods of learning, peer and Rubber Duck, I believe the process is made far easier as well as more effective. 

Happy learning!






Peer Learning in Higher Education: Learning From & With Each Other, edited by David Boud, Ruth Cohen & Jane Sampson. Published by Kogan Page Limited 120 Pentonville Road, London N1 9JN, UK and Stylus Publishing Inc. 22883 Quicksilver Drive Sterling, VA 20166-2012, USA. http://www.styluspub.com/ Copyright © David Boud, Ruth Cohen, Jane Sampson and individual contributors, 2002.

MOHAMMAD REZA DEHGHANI, MITRA AMINI, JAVAD KOJURI, PARISA NABEIEI
J Adv Med Educ Prof. 2014 Apr; 2(2): 71–76.