<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sydney Garay on Medium]]></title>
        <description><![CDATA[Stories by Sydney Garay on Medium]]></description>
        <link>https://medium.com/@sgg2123?source=rss-ee61af613d5f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*dDu_zoxpVLOsfUlhNVqxMA.jpeg</url>
            <title>Stories by Sydney Garay on Medium</title>
            <link>https://medium.com/@sgg2123?source=rss-ee61af613d5f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 14:34:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sgg2123/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Arrow Functions in JavaScript]]></title>
            <link>https://medium.com/@sgg2123/arrow-functions-in-js-ff54f558185b?source=rss-ee61af613d5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff54f558185b</guid>
            <category><![CDATA[arrow-functions]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[methods]]></category>
            <category><![CDATA[iterators]]></category>
            <category><![CDATA[this]]></category>
            <dc:creator><![CDATA[Sydney Garay]]></dc:creator>
            <pubDate>Mon, 25 Jun 2018 15:52:23 GMT</pubDate>
            <atom:updated>2018-07-31T17:19:43.507Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re learning JavaScript, you’ve probably encountered arrow functions, which use arrow notation (=&gt;) to concisely define a function. They can be very handy but it is important to know when to use them, and when not to use them. Take a look at an example of an arrow function below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*QepN3KidZsLaNM6RfwfiiQ.png" /></figure><p>In the above example, an array of names is defined and we are trying to find and log the name equal to “Waldo” using a the .find method. The .find method accepts an argument of a callback function, and here we are passing in an arrow function. The left side of the function is an individual element of the array which we are iterating over, and the right side is the condition upon which we want to return the element. When the condition first evaluates to true, the element that triggered the true statement will be returned as we see above. We will explore the why arrow functions are commonly used as callback functions for iterator methods such as .find, .map, and more.</p><p><strong>Benefits of using arrow functions:</strong></p><ul><li>They have shorter syntax than function expressions</li><li>They have implicit return statements</li><li>They retain scope of the method declared in when used as inner functions</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*uM16FiDI04Gj-iJbNwOY-w.png" /><figcaption>Arrow functions shorten the syntax of callback functions for iterator methods.</figcaption></figure><p>In the above example, we can observe both of these benefits to using arrow functions. We wrote two expressions to log an array of the lengths of our cat names. The first uses a simple function expression, and the second an arrow function. The syntax of the second expression is shorter and more clear with the implementation of our arrow function. We can also observe that we did not have to explicitly write “return” in our arrow function as we did in our function expression. If we had left “return” out of our function expression we would see this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*kpjdGGDbWOX1a7N3EtVFrQ.png" /><figcaption>The callback function returns undefined because we did not specify what to return.</figcaption></figure><p>This is because a function in JavaScript will return undefined unless we tell it to return something — that is, <strong>regular function expressions use explicit returns</strong>. The callback function in line 11 needs to return something for .map to add it to the new array.</p><p><strong>Implicit vs. explicit returns with arrow functions:</strong></p><p>The arrow function in line 8 is in fact returning the lengths of the cat names, even though we did not explicitly write “return.” This is because <strong>arrow functions use implicit returns when written without curly brackets.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*qnkb3PuOBY4TkxP6EFEoKA.png" /><figcaption>Return is <strong>implicit</strong> for arrow functions without curly brackets.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*MTuz0CqRAwcq-mB5eNPSwA.png" /><figcaption>Return is <strong>explicit</strong> for arrow functions with curly brackets.</figcaption></figure><p>The two arrow functions above behave exactly the same way but use slightly different syntax: use of curly bracket notation in an arrow function means that return will have to be explicit. We can see that the notation in line 8 is more clear and concise, however, we may choose to write an arrow function with curly brackets if we wanted to add more lines of code before the return such as a debugger, like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eSbliUD9ON3sKoY0Oiq5vA.png" /><figcaption>Using curly brackets with an arrow function allows us to add more lines of code before the return.</figcaption></figure><p>Note that if you write an arrow function with curly brackets but do not return anything, the function will return undefined (just like a regular function as we saw in line 11).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*XGFDJjNdGFCYoCaKdLUvvw.png" /><figcaption>We are missing an explicit return with this arrow function using curly brackets.</figcaption></figure><p><strong>Arguments for arrow functions:</strong></p><p>When there is just one argument, we can write an arrow function with the arguments on the left side of the arrow, either enclosed or not enclosed by parentheses. The example below shows the many ways we could write the same arrow function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lUAVP1-rMMdvlgWPJw4Idg.png" /></figure><p>When there are multiple arguments, however, we need to enclose them in parentheses:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dRMzwNRamCwxO3VtHbKGvQ.png" /><figcaption>With more than one argument, parentheses are required.</figcaption></figure><p>With no arguments, we can use empty parentheses instead:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wG8kVFQqHL-N4hwRXeIM3A.png" /><figcaption>The arrow function will be invoked for every element in cats, but no argument is passed in.</figcaption></figure><p><strong>Arrow functions and “this”:</strong></p><p>Unlike a regular function expression, if we use an arrow function inside another function, the arrow function retains the scope of the method it was declared in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mqVHS7mvwXjTHqkFnqEr1Q.png" /></figure><p>In the above example, a function expression is invoked from another function, so “this” is global. <strong>The function expression rebinds the value of “this” to the global context.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kehRcVSVKdjQVUbpJgJfFQ.png" /></figure><p>Writing “.bind(this)” after our inner function expression binds the context of that function to the context of the method it was declared in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lfy7LA3uTTIiUzgKG49bsQ.png" /></figure><p>If we instead use an arrow function for the inner function, we do not have to bind the context of that function (and thus, the value of “this”), because arrow functions retain the scope of the method they were declared in. That is, <strong>arrow functions do not rebind the value of “this” because they do not have their own “this.”</strong></p><p><strong>Constraints of using arrow functions:</strong></p><ul><li>They are not suited for object-methods</li><li>They cannot be used as constructors</li><li>They are anonymous</li></ul><p>Since arrow functions do not rebind the value of “this,” when using an arrow function as an object method, “this” will point to the parent scope. Thus, we cannot decrement fatCat1&#39;s weight using “this” in an arrow function method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UsY1MI9kMgnEWQ92LUFoLA.png" /></figure><p>Instead, we should use regular function expressions for object methods that utilize “this.” We see below that using by a function expression instead, “this” is bound to the scope of fatCat2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tv2XDBi07MK1OBt-U6CX0w.png" /></figure><p>It is also important to note that because arrow functions do not rebind “this,” they cannot be used as constructors. Using an arrow function as a constructor will result in the error shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*92foZRllVeMhRuGAnQMMsw.png" /></figure><p>Finally, since arrow functions are anonymous, they can be harder to debug. We can, however, assign them to a variable for more clarity in our error messages, as we saw in the above example.</p><p><strong>Conclusion:</strong></p><p>Arrow functions are a very useful tool for specific situations: they are best suited for non-method functions, any situation that requires “this” to be bound to the context rather than the function itself, and callback functions for iterator methods. However, they are anonymous, poorly suited for object methods, and cannot be used as constructors.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff54f558185b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[API’s and Ruby]]></title>
            <link>https://medium.com/@sgg2123/apis-and-ruby-7675144f3391?source=rss-ee61af613d5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/7675144f3391</guid>
            <category><![CDATA[servers]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[object-oriented]]></category>
            <dc:creator><![CDATA[Sydney Garay]]></dc:creator>
            <pubDate>Wed, 30 May 2018 17:53:47 GMT</pubDate>
            <atom:updated>2018-05-30T17:54:27.728Z</atom:updated>
            <content:encoded><![CDATA[<p>As a beginner programmer, I was unfamiliar with the term API and didn’t know much other than it stood for <strong>Application Programming Interface</strong> (thanks google), and that I had once used one to pull a bunch of information about Star Wars characters for a command line application I was building. So to me, an API was a word for fancy database that I access through a URL and can utilize to get data for my Ruby applications. In fact, Ruby on Rails is commonly used to <em>build</em> API’s, rather than just use them for data. You may be familiar with API’s built by companies for clients, such as the Star Wars API, but there is a broader meaning of the term API. In fact, we interact with API’s every time we make a browser request (type a site into our browser and press enter).</p><p><strong>What is an API?</strong></p><p>An <strong>Application Programming Interface</strong> is a set of code that governs the way software interacts with other software — it is the communication between the software that facilitates interaction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*SsYRqMfjqssgGNwGPYDz9g.png" /><figcaption><a href="https://computersciencewiki.org/images/b/bc/Api.png">https://computersciencewiki.org/images/b/bc/Api.png</a></figcaption></figure><p>Essentially, an API provides a set of public classes, methods, and properties that define how the software will interact.</p><p><strong>How are they used?</strong></p><p><em>Pure Data Responses:</em></p><p>Companies frequently offer API’s to their customers, for example, google offers the google books API. This means that the company has dedicated certain pages (URL’s) to returning <em>pure data responses. </em>This is the type of API that I was familiar with, and had used to make a command line application about Star Wars characters. The image below shows a sample response to an API request to the Swapi API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7OBCUdhmTZWKykdDjiAr5Q.png" /></figure><p>The API allows you to search and view characters with JSON over HTTP. As you can see, the information is a hash with many keys and values, ready to be used in code. In our Ruby application, we can access this data by making a request to the API as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*qRzHdcEr9PzAvfWGsLCpTw.png" /></figure><p>The above method “make_request” will accept a URL as a string and use the Ruby gems “rest-client” and “json” to get and parse the response. The data hash will now be available to us as the output of our “make_request” method. There are multiple URLs dedicated to returning pure data responses that can be queried using paths such as “people/1/” or “planets/3/.” When one of these paths is passed in as part of the URL, the API returns the corresponding data hash. The API call represents the specific query to the organization — in the above case, to show the first page of data about people.</p><p><em>API’s in applications:</em></p><p>The “A” for application can refer to either the whole app/server or one piece of the app/server. Therefore, any piece of software that can be isolated from the rest likely has an API. For example, if you wanted to incorporate a dictionary into an application you’re building to shorten words in a tweet, you would need an API to define how that dictionary would interact with the rest of the code — that is, how, when, and where the dictionary would be used.</p><p>Another interesting example of API’s is in object oriented programming — the classes and methods in Ruby’s core library use API’s to communicate with the rest of your code. Each API is a set of public methods used to interact with other objects in your application.</p><p><em>API’s and Remote Servers:</em></p><p>API’s are used all over the web, and in fact, <em>every time you access a web page, you interact with a remote server’s API. </em>This is because the remote server contains a set of code that governs your browser’s interaction with the site. Essentially: when you make a request to a web page, you are a client interacting with that remote server’s API. An API isn’t the same as a remote server though, that is, twitter.com isn’t an API, but rather the API is the part of the server that receives requests and sends responses (i.e. the controller). When you make a request to twitter, you are making a request to the API. The response is formatted within the layout of twitter, and not as raw data, but the API still receives the request and sends the client a response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pfNpECpv1hLW0UAqWIncnQ.png" /><figcaption><a href="http://media2.govtech.com/images/940*603/api_infographic_smartfile_crop.jpg">http://media2.govtech.com/images/940*603/api_infographic_smartfile_crop.jpg</a></figcaption></figure><p><strong>Making API’s with Ruby on Rails</strong></p><p>I’ve been told that as I dive deeper into coding I will also be using Ruby on Rails to create API’s, rather than just utilize API’s to pull data from the web. Although I haven’t yet had the chance to do so, this would be useful so that you can make data from your site available to other developers, for example, an you may want to make an API for all of the data from your travel site available to developers who want to get stats about certain destinations or hosts.</p><p><strong>Conclusion</strong></p><p>You’ve been using API’s most of your adult (and probably earlier) life without even knowing. At its root, an API is a set of code that governs how two pieces of software interact with each other. In practice, API’s govern how remote servers respond to requests and how parts of our applications work with one another.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7675144f3391" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[@/@@ vs. self in Ruby]]></title>
            <link>https://medium.com/@sgg2123/vs-self-in-ruby-1d4d88170?source=rss-ee61af613d5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d4d88170</guid>
            <category><![CDATA[self]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Sydney Garay]]></dc:creator>
            <pubDate>Wed, 09 May 2018 16:51:24 GMT</pubDate>
            <atom:updated>2018-05-09T17:54:05.005Z</atom:updated>
            <content:encoded><![CDATA[<p>When I first started out with object oriented Ruby, I noticed that I could seemingly use instance and class variables interchangeably with their self methods. That is, it seemed like I could arbitrarily replace @instance_var with self.instance_var, and @class_var with self.class_var in instance and class methods respectively, and get the same expected output from my methods. It turns out that @var/@@var and self.var are actually communicating with Ruby in distinct ways, and they <em>may</em> return the same value but this is not always the case. Let’s take a closer look at @/@@ and self to unpack the difference.</p><p><strong>What is @/@@?</strong></p><p>There are 4 types of variables in Ruby: local , global, instance, and class. The @ symbol before a variable tells Ruby that we are working with an instance variable, and @@ before a variable tells us we are working with a class variable. We use @ before a variable in <em>instance</em> methods within a class to tell Ruby to access that attribute (instance variable) of the instance. Using @instance_var makes instance_var accessible to all instance methods. Similarly, we use @@ before a variable in <em>class </em>methods to tell Ruby to access that attribute (class variable) of the class. Using @@class_var makes class_var accessible to the entire class. When we use @instance_var or @@class_var, we are accessing that instance or class variable directly.</p><p>Here is an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tx6asr8R9LKqADpSos5DlA.png" /></figure><p><strong>What is self?</strong></p><p>You may have noticed the word self in the definition our class methods. Every object in Ruby has and is aware of its self. The keyword self in Ruby enables you to access to the current object — the object that is receiving the current message. The word self can be used in the definition of a class method to tell Ruby that the method is for the<em> self, </em>which is in this case the class. Using self <em>inside</em> an instance or class method refers to the same object the method is being called on, i.e., and instance and class respectively. In an instance method such as #say_hi, using self.instance_var tells the object (in this case, an instance of a class) to send itself the message “instance_var” which, depending on how the class is written, can return @instance_var or something else. In a class method, using self.class_var tells the object (in this case, a class) to send itself the message “class_var” which, depending on how the class is written, can return @@class_var or something else.</p><p>Here is an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ooXXPO5xi1yVH03LFdcdog.png" /></figure><p>Wait a minute… didn’t we just replace @instance_var with self.instance_var and @@class_var with self.class_var, and get the same output for our say_hi methods?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*5Eyp12oUMn7yRH2_UHCkJA.png" /></figure><p>It’s not a trick. Let’s find out why we can do this.</p><p><strong>Why can we use them interchangeably here?</strong></p><p>In this example, we were able to use @class_var and @instance_var interchangeably with the methods self.class_var and self.instance_var because those methods return @class_var and @instance_var respectively. Instead of directly calling the variable, we are going <em>through </em>the #class_var and .instance_var methods, which in this case, also return @@class_var and @instance_var.</p><p><strong>So is there a difference?</strong></p><p>Yes! While using @ or @@ to access an instance or class variable, we are telling Ruby to access that variable <em>directly</em>. In fact, even if we had instead just initialized @instance_var and @@class_var with those strings, and not written methods #instance var and .class_var to access those variables, we would still be able to call them directly in our say_hi methods since we are directly accessing a variable we initialized.</p><p>Take a look:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DGhiZztyLr-D-u1FHBGpNA.png" /></figure><p>What if we initialize @instance_var and @@class_var and then try to call them using self.instance_var and self.class_var in our say_hi methods.</p><p>Look at the error we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fG1PllpX147FnnhODSpVLg.png" /></figure><p>That’s helpful feedback. It looks like Ruby is telling us that when we wrote self.instance_variable, Ruby looked for an instance method #instance_variable and didn’t find it. That’s because it doesn’t exist! The same thing happened with self.class_var: we don’t have a class method .class_var, so we get an error. So in summary, we can only use @instance_var and @@class_var interchangeably with self.instance_var and self.class_var <em>if </em>those methods exist and they return that instance or class variable.</p><p><strong>Which one should I use?</strong></p><p>You might be thinking: “Why should I go through an instance or class method to get to a variable when I could just access that variable directly?” And you’d be right. There’s not much point going through a method <em>unless </em>that method returns something other than the original variable (this is a common case because we tend to write methods that manipulate our variables, rather than just return them or set them to a constant value).</p><p>We typically want give ourselves reading and writing access to an instance variable, so that we can use it in other methods and set its value. For this we can use an attr_accessor, which is shorthand for writing a setter and getter method for the instance variable.</p><p>Lets look at a more realistic example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MdNgtmjnfk05Pd0-nxDrYQ.png" /></figure><p>Since we have an attr_accessor for our instance variable @age, we can still use self.age in the place of @age in out #age_in_dog_years method, however we can see that there isn’t any added clarity or functionality in going through the #age method rather than calling the instance variable directly. Similarly, since we wrote a method self.all that returns @@all, we could have used self.all in the place of @@all in our .num_people method, but again, there is no increased clarity or functionality in going through the self.all method rather than calling the class variable directly.</p><p>We do notice though, that our methods #age_in_dog_years and .num_people cannot be accessed via an instance variable because they are <em>methods </em>that rely on other instance variables.</p><p><strong>Do we need to explicitly say self?</strong></p><p>We noted that in our method #age_in_dog_years, we could have called self.age instead of @age. Since we are in an instance method, Ruby notices that you didn’t pass “age” in as an argument, or define a variable “age” within the instance method, so it looks for an <em>instance method </em>called “age.” It finds the instance method #age (through the attr_accessor), assumes that this is what you meant, and uses the return value of the method, which is @age. We also noted that in our method .num_people, we could have called self.all instead of @@all. Similarly, since we are in a class method, Ruby notices that you didn’t pass “all” in as an argument, or define a variable “all” within the class method, so it looks for a <em>class method </em>called “all.” It finds the class method .all, assumes that this is what you meant, and uses the return value of the method, which is @@all.</p><p>It would look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OjLFK5M1cCfQndMV9EgWNg.png" /></figure><p><strong>A final note:</strong></p><p>Say we decided that instead of calling our class variable “all” we wanted to call it “people.” If we used @@all everywhere instead of self.all, we could have to change all those places to say @@people. If instead we used self in the following way to call the class method .all rather than the actual class variable, we would only have to change it in our .all method. Notice how we could even use self to call the class method .all within initialize, if we say “self.class.all” rather than “self.all.” We do this to tell Ruby we are talking about the class method “all,” rather than the instance method #all (which doesn’t exist).</p><p>Take a look:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZKjzMKRuUecdSw2UzP6fxQ.png" /></figure><p>In this way, we can make our code more robust, so that if one variable name changes, we do not have to make many changes throughout our program. On the other hand, calling instance and class variables is more explicit, and doing a find and replace all is not difficult. So at the end of the day, if your method is returning the variable you want, it comes down to user preference whether you use @/@@ or self.</p><p><strong>Conclusion</strong></p><p>So there you have it: use instance or class variables when you want to access the variable directly, and use self to call an instance or class method. Sometimes instance or class methods return the variable you want. In this case, it’s up to you which to use. However, instance and class methods frequently manipulate instance and class variables, thus calling those methods on objects via the self keyword is common and useful in Ruby.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d4d88170" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>