<?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: Charlie E. Paterson</title>
    <description>The latest articles on DEV Community by Charlie E. Paterson (@cpatercodes).</description>
    <link>https://dev.to/cpatercodes</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%2F265606%2Fae0b64b3-e925-41cb-8d44-8a906b42341e.jpg</url>
      <title>DEV Community: Charlie E. Paterson</title>
      <link>https://dev.to/cpatercodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cpatercodes"/>
    <language>en</language>
    <item>
      <title>A Rubyist's guide to Javascript</title>
      <dc:creator>Charlie E. Paterson</dc:creator>
      <pubDate>Mon, 17 Feb 2020 15:04:48 +0000</pubDate>
      <link>https://dev.to/cpatercodes/a-rubyist-s-guide-to-javascript-5ank</link>
      <guid>https://dev.to/cpatercodes/a-rubyist-s-guide-to-javascript-5ank</guid>
      <description>&lt;p&gt;To start this post of, I feel it fitting to put one popular misconception to rest: Javascript is not, in fact, related to Java. Mine, at least, is beginning to seem like a distant cousin of working script (and sometimes, of the kind that does things!) I've come to learn a couple of things about the language along the way, and similarities/differences to Ruby.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Semi-colons, semi-colons everywhere!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At the end of most lines of code that are run, the developer needs to put a semi-colon unlike in Ruby. Exceptions can be made, however, when defining a function (what a rubyist would call a method) or even some simpler logic. &lt;/p&gt;

&lt;p&gt;This is less extreme and consistent than languages such as C++ which outright ignore whitespace and only move to the next line after a semi-colon, but it appears to nonetheless be possible to use a semi-colon in place of a linebreak (as evidenced by some rather unsightly source files... looking at you, JQuery!).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;..Don't forget empty brackets!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If I've learnt anything from struggling with some particularly nerve-wracking bugs, it's that you need parentheses in front of any method call more complex than to return a stored value. Your method doesn't take arguments? Empty parentheses it is! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C'est ne pas 'puts'&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Firstly, as a Rubyist you may be familiar both with &lt;em&gt;puts&lt;/em&gt; (or sometimes &lt;em&gt;print&lt;/em&gt;) for outputting text and with &lt;em&gt;p&lt;/em&gt; for displaying the value of a variable during specs. &lt;br&gt;
When first learning of &lt;em&gt;console.log&lt;/em&gt; in javascript, many will see parallels to the former but it is in fact in between the two. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The actual 'puts' of JS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you really, really want to say something to the user, you want to use either &lt;em&gt;document.GetElementById(element_id).innerHTML = desiredText&lt;/em&gt;, (swap out for &lt;em&gt;GetElementsByClassName&lt;/em&gt; or &lt;em&gt;GetElementsByTagName&lt;/em&gt; as desired) to manipulate the content inside a HTML element.&lt;br&gt;
Because you see, reader, Javascript is a front end language intended to manipulate HTML (and sometimes CSS).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function? Class? Was this ever meant to be?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While the most recent standard for Javascript (ES6) does have a class syntax of sorts (and has long had a syntax for 'prototypes' of functions), the distinction between classes and methods which exists for many backend languages doesn't translate as cleanly on to JavaScript for the most part as a matter of design. Functions are added to a 'class' by means of &lt;em&gt;className.prototype.functionName = function(){ code here }&lt;/em&gt;, and instances of said class defined by &lt;em&gt;var instanceName = new className&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Javascript, ultimately, is a front end tool intended to manipulate HTML and CSS on the fly.&lt;br&gt;
Few could have anticipated the complexity of logic which it has evolved to be able to take on - especially of the kind that traditionally would be relegated to back end logic - but methods exist to create essentially the entirety of a web application's logic in Javascript.&lt;/p&gt;

&lt;p&gt;It is for this reason I think it is felicitous to touch on two main approaches that can be taken: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Front end single page web app:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Usually the fact that pure JS can only really perform actions within the scope of the rendered page can come across as quite daunting; how on earth do you carry data entered or produced within one part of your app across the app as a whole? But what if we don't move between pages at all, and do all our logic manipulations right there and then? Well then, reader, this curse can be made into a blessing.&lt;/p&gt;

&lt;p&gt;The great thing about not moving between different pages in a web app is that you don't have to go to all the trouble of constantly sending out requests to the server. &lt;br&gt;
This can be a lifesaver for an app's users (in figurative terms, but sometimes literal depending on what your app &lt;em&gt;does&lt;/em&gt;) if it just so happens that their internet is pretty terrible or their provider charges a lot for that precious extra traffic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Node.js:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While Node.js is a technology I must still delve further into and learn, its main appeal is that it allows both the frontend and the backend logic to be unified under a single language. From the outset, this makes it far easier to take calculations made by interactive elements on the frontend and update records held on the server-side accordingly, and in turn carry these between pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript is a surprisingly versatile - and at times confusing - language which has grown from a controlling medium for dynamic frontend elements to hosting capabilities on the level of a backend language.&lt;/p&gt;

&lt;p&gt;It is by understanding its history and the way its scope has grown profoundly from its original intended purpose that we can understand the quirks and conventions that distinguish it from other languages. There are many more I could list, but I wanted to cover what was most striking to me about JS coming from a Ruby background.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>On the front-end frontier: my venture into web-apps</title>
      <dc:creator>Charlie E. Paterson</dc:creator>
      <pubDate>Mon, 02 Dec 2019 12:38:11 +0000</pubDate>
      <link>https://dev.to/cpatercodes/on-the-front-end-frontier-my-venture-into-web-apps-4ac7</link>
      <guid>https://dev.to/cpatercodes/on-the-front-end-frontier-my-venture-into-web-apps-4ac7</guid>
      <description>&lt;p&gt;Having a grasp on ruby is fantastic and such, but this raises the question of what next to do with one's impressive smorgasbord of classes, communicating the way they should with the right methods and having braved every possible edge case. &lt;/p&gt;

&lt;p&gt;That is to ask, how do we now make something that begins to actually look a little bit like a website or, better still, a web app? &lt;/p&gt;

&lt;p&gt;...To be honest, I'm still not entirely sure myself, but there are a couple basics and tricks to building web apps in Ruby which I wish to cover.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sinatra
&lt;/h2&gt;

&lt;p&gt;Sinatra is a ruby gem which serves as a framework for implementing ruby code as the backend language - that is, the language through which objects, the data they hold and relationships between pages are controlled. When elements coded in HTML call for either a GET or a POST request (links typically are confined to GET), it's typically in this backend code that any page redirections or manipulations of form or other data are written. &lt;/p&gt;

&lt;p&gt;One of my personal favourite things to do here - which I took some time to warm to - is the Post/Redirect/Get pattern, wherein a post request assigns input to variables, then redirects to a page with a get request.&lt;/p&gt;

&lt;p&gt;For example, let us say we wanted to take a name and email address and submit them to a list page, but didn't want to risk submitting the data anew every time we refreshed the target page? In the case of, say, a shopping app where such could lead to duplicate payments?&lt;/p&gt;

&lt;p&gt;One could have a form submission for a name and email address activate a &lt;em&gt;post '/submit' do end&lt;/em&gt; block which takes the text input into the form and stores the contents of each parameter in variables. &lt;br&gt;
If one chooses to &lt;em&gt;enable :sessions&lt;/em&gt; within their core web app file, these variables can be values tied to keys within the session hash created by sessions.&lt;/p&gt;

&lt;p&gt;Now, the post block can redirect to another page address with the redirect method (in this hypothetical, &lt;em&gt;redirect '/list'&lt;/em&gt;), tied to a &lt;em&gt;get '/database' do end&lt;/em&gt; block that takes those session key-value pairs and does whatever it is we want to do. This particular feature I'm not quite yet sure how I would build, but there is a more pressing question that must be answered first. Namely, how do we take data from the backend into the front end?&lt;/p&gt;

&lt;h2&gt;
  
  
  Interpolation of ruby code
&lt;/h2&gt;

&lt;p&gt;Within a GET or POST block within the central controller, one can set instance variables, and then invoke these in the code for the associated .erb file (the file format we use for front end units).&lt;br&gt;
Given a variable &lt;em&gt;@instance_variable&lt;/em&gt; in a GET/POST block, followed by &lt;em&gt;erb(:page)&lt;/em&gt;, one can then print the value of the variable into &lt;em&gt;page.erb&lt;/em&gt; using the syntax &lt;em&gt;&lt;/em&gt;. &lt;br&gt;
One can again make use of the session hash by enabling sessions if one wishes to make these variable values available across the entirety of the web app session.&lt;/p&gt;

&lt;p&gt;Alternatively, one can make certain elements within a view in the web app subject to certain conditions determined through the backend. &lt;br&gt;
The way to implement if statements on the front end is to open them with &lt;em&gt;&amp;lt;% if condition %&amp;gt;&lt;/em&gt;, enter the elements which you only want to execute on the page if the condition is true, and close with &lt;em&gt;&amp;lt;% end %&amp;gt;&lt;/em&gt;. You can also add elsif and else clauses with &lt;em&gt;&amp;lt;% elsif other_condition %&amp;gt;&lt;/em&gt; and &lt;br&gt;
&lt;em&gt;&amp;lt;% else %&amp;gt;&lt;/em&gt; respectively.&lt;/p&gt;

&lt;p&gt;These are just some of the ways in which you can invoke ruby code directly on to the front end in order to regulate the presence or presentation of content.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;It can be tedious at times to continuously have to close own and restart your web app in order to fully test out its functionality. You can use software such as Shotgun to make this easier for you, although I personally have had errors with Shotgun as while I can launch my web app successfully from the command line with it I find that it raises &lt;em&gt;Sinatra doesn't know this ditty&lt;/em&gt;. It is, nonetheless, a useful piece of software for ease of interaction with and fine-tuning of your web app. Rackup is also recommended.&lt;/p&gt;

&lt;p&gt;Ultimately, the most important thing to do is write adequate spec tests for your code. Thankfully, there is a testing framework known as Capybara that can work with Rspec and other backend ruby TDD frameworks.&lt;/p&gt;

&lt;p&gt;A full cheatsheet and documentation for working with Capybara can be found at &lt;a href="https://devhints.io/capybara"&gt;https://devhints.io/capybara&lt;/a&gt; and contains tests for the presence of links, buttons and content amongst others. &lt;br&gt;
The practice I am familiar with for front end feature testing of .erb files is that one creates a separate /features directory within their spec directory. &lt;br&gt;
Here one should endeavour to have a separate spec file for each view that they create and ensure to be thorough in test coverage. This way one can minimise the number of times they need to reload the page if they cannot find software that can successfully do this for them (I can only speak from personal experience). &lt;/p&gt;

&lt;p&gt;If one wants to test out tweaks to elements or CSS styling on the fly, google chrome does offer the tools for this. Simply select the button in the top right with three dots vertically stacked, scroll down to 'more tools' on the menu that presents itself, and click on 'developer tools'. This can also be a useful accomplice to your core web development learning resources as it allows you to peak into some of the source code and allow you to inspect specific elements to better understand how they were built.&lt;/p&gt;

&lt;p&gt;These are just a few tips which I have managed to take on in my current and very basic understanding of building web apps with ruby.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Getting into programming with classes</title>
      <dc:creator>Charlie E. Paterson</dc:creator>
      <pubDate>Sun, 17 Nov 2019 23:53:07 +0000</pubDate>
      <link>https://dev.to/cpatercodes/getting-into-programming-with-classes-3nad</link>
      <guid>https://dev.to/cpatercodes/getting-into-programming-with-classes-3nad</guid>
      <description>&lt;p&gt;There's a certain threshold that one must eventually cross when getting to grips with any language (although I currently can only speak from a basic grasp on ruby). You might be thinking, iterating over hashes perhaps? Or is this wrapping our heads around control flow and how to get data to travel properly between methods? Or perhaps even committing to memory - and coming to grips with the workings behind - methods &lt;em&gt;with&lt;/em&gt; hashes?&lt;/p&gt;

&lt;p&gt;Well, no, not quite. But hashes, arrays, strings and so forth respond to the methods that they do so because these methods are part of their &lt;em&gt;class&lt;/em&gt;. &lt;br&gt;
The array class has it's own methods, the string class has it's own methods...and can even have a range of array methods called upon it, but that's beside the point (even if all too useful!). &lt;br&gt;
For you see, reader, one can make their &lt;em&gt;very own classes&lt;/em&gt;, as follows:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;class SomeClass&lt;/em&gt;&lt;br&gt;
  &lt;em&gt;def initialize(argument, other_argument)&lt;/em&gt;&lt;br&gt;
    &lt;em&gt;@argument = argument&lt;/em&gt;&lt;br&gt;
    &lt;em&gt;@other_argument = other_argument&lt;/em&gt;&lt;br&gt;
  &lt;em&gt;end&lt;/em&gt;&lt;br&gt;
&lt;em&gt;end&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One might be lead to think there's little much else to making their &lt;em&gt;very own methods&lt;/em&gt; but this, too, has some exciting quirks that it's important to get a handle on if one wishes to properly harness the potential of custom classes!&lt;/p&gt;

&lt;p&gt;As you may have noticed, my example contains an initialize method - this is standard practice if one wishes to individualise the data held by instances (objects) belonging to the class (such as name, age, temperature, you name it!), and will run whenever you do &lt;br&gt;
"some_object = SomeClass.new"! &lt;/p&gt;

&lt;p&gt;The '@' symbol I've placed before the variable names in the body means that their assignment in the method will apply across the entire instance, rather than just that one method (remember that the only data methods will usually provide outside of themselves is that which is 'return'ed).&lt;/p&gt;

&lt;p&gt;Of course we could set about coding methods to return each of these instance attributes we have set, but a much quicker way to be able to return them is as follows:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;class OtherClass&lt;/em&gt;&lt;br&gt;
   &lt;em&gt;attr_reader :argument, :other_argument&lt;/em&gt;&lt;br&gt;
   (initialize variable and so on)&lt;br&gt;
&lt;em&gt;end&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With attr_reader, you're all set to return the value of any of the chosen instance attributes without having to code methods to return each and every one of them. However, if you want to be able to change these values you need attr_writer, and to do both you'll need attr_accessor.&lt;/p&gt;

&lt;p&gt;From here you can write methods to make your classes do all kinds of things: maybe one of these instance variables is an array, and you want a method to push instances of some other class into it? Or maybe you want said instance to have a method to push itself when called upon it? Maybe a class called 'Villain' or 'Student' could hold a whole slew of information in the form of class attributes, allowing a whole different approach to the student directory project of yesterweek?&lt;/p&gt;

&lt;p&gt;While I do have to admit that custom classes do still feel ever-so-slightly ominous in their novelty, I'm looking forward to really playing with the full roster of their capabilities in my journey through the world of code.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>classes</category>
      <category>methods</category>
    </item>
    <item>
      <title>Getting ready to code for real: Thoughts and things to do</title>
      <dc:creator>Charlie E. Paterson</dc:creator>
      <pubDate>Wed, 06 Nov 2019 16:56:39 +0000</pubDate>
      <link>https://dev.to/cpatercodes/getting-ready-to-code-for-real-thoughts-and-things-to-do-4jbm</link>
      <guid>https://dev.to/cpatercodes/getting-ready-to-code-for-real-thoughts-and-things-to-do-4jbm</guid>
      <description>&lt;h2&gt;
  
  
  My learning and challenges so far
&lt;/h2&gt;

&lt;p&gt;Over the last couple weeks I've learnt a thing or two not only about the core workings and features of ruby as a language, but also about how to manage all my files and the git repositories I keep them in through the command line. &lt;/p&gt;

&lt;p&gt;While at first I was getting lost in the march of 'no such file or directory' messages, I have now come to a basic understanding of navigating files on the command line. I have even completed a fun 'murder mystery' challenge which really emphasised the use of grep and related commands to extract and store relevant information to solve the mystery.&lt;br&gt;
One can imagine how hard it must be to search a file for a crime scene report buried within the full text of 'Alice in Wonderland' without some command line sorcery!&lt;br&gt;
While I still have many more tips and tricks to pick up in order to really make full use of the command line, these should come in time.&lt;/p&gt;

&lt;p&gt;I've also tried my hand at doing a range of interesting and challenging things with code itself, namely ruby. The most unusual of these so far was to take an array containing various strings representing 'levels' of a river and create a game wherein the user (represented by 'P') navigates down the river and must avoid bumping into ravenous crocodiles (represented by 'C's). At this point I have some basic understanding of all the main structures (arrays, hashes, loops) but still have yet to really make full use of classes, lambdas or procs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A couple things so far that have really stuck out or been useful to me about ruby as a language are:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Many methods reserved for arrays can also be used on strings, treating them as 'arrays' of characters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are all manner of interesting alternative syntaxes, such as the format &lt;em&gt;'condition ? "outcome_a" : "outcome_b"'&lt;/em&gt; for an if-else statement or the use of &lt;em&gt;'array &amp;lt;&amp;lt; element'&lt;/em&gt; instead of &lt;em&gt;'array.push(element)'&lt;/em&gt;. However, they are only more elegant in &lt;em&gt;some&lt;/em&gt; contexts!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The language feels a lot more elegant and approachable than Java. The latter I hope to get used to in time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;I've also noticed a couple things I still need to get used to:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Making sure I don't use '=' where I should be using '==', and proofreading in general&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Getting some more built-in methods under my belt! There are many with a number of valuable uses, but I don't know many off by heart so my capabilities are still limited.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ensuring proper flow of control with many methods. The penultimate exercise of weeks 2-3 of my precourse, programming a game of blackjack, challenged me a lot for this reason. &lt;br&gt;
I found it difficult to ensure that methods were communicating with each other properly in terms of transferability of variables and return of information from variables.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My current project
&lt;/h2&gt;

&lt;p&gt;More recently as part of my pre-course, the time has come for me to try my hand at building something more elaborate: a directory of student names! So far, I have implemented and played with a couple of features.&lt;/p&gt;

&lt;p&gt;A very basic way to do this with only rudimentary coding knowledge would be to 'puts' each and every name as a string on a new line, and then 'puts' a string declaring the number of students.&lt;/p&gt;

&lt;p&gt;The intuitive and first model for such a program is instead to store the names all in an array as I did, and iterate 'puts' over each of these when we run the program. &lt;/p&gt;

&lt;p&gt;But we can also, in turn, make the student count statement dynamic and able to stay accurate to the actual student count automatically. &lt;br&gt;
This I did by calling '.length' on the student array within the string via interpolation, which resembles this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;'puts "We currently have #{array.length} students total"'&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I went even further and created three seperate methods for printing a header, the list of students, and a footer stating the number of students respectively.&lt;/p&gt;

&lt;p&gt;But what if, in turn, we wanted to include some extra information? Say I wanted to store not just a name, but a cohort month? For this, a possible solution is to store &lt;em&gt;hashes&lt;/em&gt; in our array. &lt;/p&gt;

&lt;p&gt;Hashes differ from arrays in that you typically call the 'values' not by numerical index, but by keys. These can be integers, strings, symbols and other objects.&lt;br&gt;
Multiple different keys in an array can be assigned to the same value, but in this particular case I made use of the fact that the same key can be used to return a different value per array (two different values on the same key doesn't make as much sense &lt;em&gt;within&lt;/em&gt; a hash).&lt;/p&gt;

&lt;p&gt;A 'name' key can be used on each hash representing each student to return their names, and a 'month' key to return their cohort. &lt;/p&gt;

&lt;p&gt;From this we can fill our array with a hash per student, instead of strings of their names, and plug the same keys into each such hash to return the value that each hash uniquely assigns to said key, as follows:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"array.each {|student| puts "#{student[name]},#{student[month]}"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I never thought I'd see the day when I built an array of hashes, over which I would subsequently iterate with a side-serving of string interpolation, but here we are. And we're not done yet!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A set of features I'm looking to implement as part of my learning exercises over the rest of the week are:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Getting user input: perhaps to add or remove students, or even change their month cohort if there's been a mix up!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Even &lt;em&gt;more&lt;/em&gt; information per student&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Centred text if possible&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An interactive menu allowing users to navigate and call the different methods of the program at will&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Saving output from the program into files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And more!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How exactly I am going to do all of this is something I am excited to find out along my journey through the world of code!&lt;/p&gt;

&lt;p&gt;~ Charlie Paterson &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
