Think Outside the Block


new.map{ |u| expert = u * experience}

You slugged it out of the park within the parameters

Being a rails developer means you are working with databases and primary keys. This also means you are going to be building out your application in a resfully CRUDY manner that follows convention. The rails motto is literally “Convention over configuration” and also in the early days “You’re not a beautiful and unique snowflake”. However the convention follows displaying primary keys in the address bar URL. If your brains didn’t just throw you a warring error it’s okay because I didn’t see it as a problem either, at first. HOWEVER if you are not a user of an application that has 100s of users you could literally just bypass a lot of logging in and validation by just typing ‘some_fake_app/10’ and login. This would give anyone user 10’s info within the app. Well come back to this problem in a bit. This information in the URL bar is a key component to rails apps.


.Try .Try .Try again. Rails methods for every problem.

**In Ruby there is a method to overcome almost everything. Then there’s rails. This is problem solving for everything on steroids. Every thing. Ev ery th ing. Like getting an unexpected nil for example? That’s fairly troublesome in that it can break your program and throw all kinds of errors. In the past I would try to be cleaver and find a way to work around every hitting a nil to avoid throwing an error and breaking the program. I watched a video a few days ago and it had you guessed it: a method for that.


MVC architecture

Model view controller is a software design pattern that is ubiquitous throughout software. From Sinatra and Rails in Ruby or Vue and Angular in JavaScript MVC gives you a scaffold for which to build your program. MVC lets you know how build out the complicated program in that a program is multifaceted and now you know here to put everything. The restaurant analogy is the best to relate MVC to the real world. What if you had 100 people that were hungry, 2 chefs, 1000 lbs of food, and 3 waiters at your disposal. Would you know how to deal with all that? Most likely you would set it up like a traditional restaurant to get these people fed. You wouldn’t spend time thinking about the best way or the most optimal way to do this. I’m not the only one to express this as a restaurant but I needed to reiterate because there was some confusion as to the way it is expressed elsewhere. So the Model in the MVC is literally data processing or data storage. It is 100% the part of your app or program that stores data or processes that data. In a Sinatra app I’m building my Model is a class(literally a table through Active Record) that stores data and pulls out that data when needed in the way it is needed. In my analogy it is literally the Cooks or chiefs. It processes the food and sends it back to the requester in the way it was requested. In my Sinatra app View is just what the person clicking on the website sees. It’s on the opposite end of Model. It shows the users stuff and lets them request stuff. In my Sinatra App the View is literally just the HMTL or HTML & ERB. You look at the view and make requests to the View. That’s it. The C in MVC in my Sinatra App is perfectly called controller. Here when the user sakes for things The C (Controller) does all the “get” and “post” action and is very CRUD_y. It will help put in the request to Create, help you Read, Manage your Update, and perform your Delete. In our analogy it is the waiter (what is grate is that the Model (cook) does not ever have any deleting and the Controller (waiter) can throw it away) and takes in user info and gives it to the cook, then brings what the cook processes back to the user. The most Important part of this blog post is that the Model is “data” meaning it stores user data such as user name and user password and then the Model processes that “data” if a user is trying to log in, checking if the password matches.


Raising Custom errors

Raising custom errors can be really exciting. It gives you a way to deal with a program when things go wrong. You cannot always predict/control what is going to happen; sometimes the internet is down/slow. Custom error handling can be simplified by just encapsulating your code into small chunks of methods that only do one thing (this is one of the reasons to do this) and ending that method with a rescue clause. By doing this ruby will evaluate this entire method as being wrapped in a begin..Rescue block and will not stop the program if an exception is raised. The program is then “Rescued”. To do so first you will have to define a custom error class that inherits from StandardError. Most errors fall under the StandardError class umbrella and have no built in rescue methods. Thus your error will be defined and will now have a way to save it. For example look at the following code.
class Custom_error < StandardError def message <<-DOC put error message here DOC end end Here a Custom_error is defined that inherites from Standard Error and has a rescue message. Now that you have a Custom_error class and an error message you can define the exception that are going to catch and resured in the encapsulated code where the method is ended with a rescue clause and the rescue message is raised def self.open_page(index_url) return doc = Nokogiri::HTML(open(index_url)) rescue Net::OpenTimeout => e raise Custom_error, e.message rescue SocketError => e raise Custom_error, e.message end In the above code Open-uri is pulling HTML out of a website and Nokogiri is scraping the website for data. However if the internet connection is slow (OpenTimeout) or the connection is not connected (SocketError) this exception will be rescued with the Custom_error message. Now this is probably not the optimal way to rescue this. For example perhaps you could offer a picture of a cat hidden in your program that is stored in the error handling as cats run the internet. The point is to encapsulate your code into chunks of small methods that end in a rescue clause so ruby will know what do if something goes wrong.


I came, I saw, I scraped

I just finished my CLI project. It was a lot of work! I got stuck, I cried, I stayed up late, I was late to work, I didn’t sleep, and I eventually won. I beat the errors, I built the code, I fixed stuff that was missing, I dryed out the code, etc etc etc. I feel like I learned so much and really that is how you learn to code. With sweat, tears, blood, and headaches I managed to shove so much code info into my brain and come out code smarter. I am totally loving the understanding of Object Oriented programming. The best thing I can do here is point out in Object Oriented Programming you need to pass around Objects to other Objects. That is Key to how to really use the language to it’s full potential. Otherwise you are just kinda doing simple math problems you can never use. The second thing I can point out is that you can encapsulate your code and reuse it over and over. I had this problem with my CLI where to keep it simple for the user I had to create multiple gets for different parts of the program. That way the user could just keep typing the same number to go deeper into the website I was scrapping. This created a problem with multiple streams of gets variables running and being difficult to track and close out. Then I realized that I could create a def method that would put out a variable that used the gets method and boom I was good to go. Now I just had to use that method in different places with different variable names! It made the code so much dryer! Is my code perfect? NO. Most definitely not. I already have a few ideas to make it better (e.g. fix the custom_error I tried to create when the internet was slow or down). I really feel much better than two months ago when I first started! Before I end this blog post I’m going to point out a huge error I had with Open-uri and nonsmoking that was very simple and misleading to debug. Open-uri and nonsmoking need to http:// at the beginning of a url to pull out HTML and scrape it. Now when you scrape a website and pull out links you may just be pulling out the tail end of the url. With binging.pry you will see the link and put it in the browser. That will mislead you because the browser will ADD THE HTTP://!!!! It will appear as if the link is valid despite the error telling you it does not exist. I spend two days on that problem. I thought It was me and something wrong with my code (part of the error had something about initialized). I coded and recoded and triple coded the same things in different ways! Errors can only guess what is wrong. Again. Open-uri and nonsmoking use http:// and without that initial http:// it does not exist to either!