I've been trying to get up at 5 am everyday this week to work on my coding, and I actually did a pretty good job except for Friday morning. I'm a huge Broncos fan and we had the Thursday night game so I didn't make it to bed until nearly midnight. After getting up at 5 am for a few days, I was just too tired and needed the extra sleep.
I did make it up at 6 am today though (even though I'm not doing the 5 am challenge on weekends) and have been working on my problems. I wrapped up my first problem which was implementing a simple Array class in Java and Ruby that has "create, delete, search, and display" methods for working with the array. I thought I would take a few minutes to at least share the things I learned and a few of the "gotcha" moments.
When trying to implement the solution in Ruby, my first stumbling block was working with & declaring the right type of variables. In Ruby, you have global variables, class variables, instance variables, & local variables. I'm going to try to briefly explain them, but to help give context I'll give a simple example of classes & objects.
If I were writing a program to control working with vehicles, I would potentially have a "Vehicles" class, and two sub-classes called "Cars" & "Trucks". Classes help control behavior and allow you to create "Objects" which all share similar behavior. I would create certain behavior in the "Vehicles" class that would apply to both Cars and Trucks, and the "Cars" and "Trucks" classes would both inherit that behavior. The reason I would do this is that both Cars & Trucks have Tires and doors, and windshields for example, and rather than specify these features in both classes, I can type it once in Vehicles and let both classes inherit those features.
Now once I have my classes, I can then create specific objects. So I have a "Cars" class that outlines the features and behaviors, but then I create a "Car" object that is the actual car itself with its own unique make, model, color, etc.
Hopefully I made that simple enough and not too confusing, but not on to explaining the variables and where I got stuck.
Global variables - are just what the name suggests, they are globally available throughout your program. So if I have a Vehicles Class and Animal Class, I might declare a variable in one of them, but I could still access that variable from the other class as well
Class Variables - class variables apply to that class & the objects within that class. So if I have a Vehicles Class and a Animal Class, and I declare a variable within the Vehicles class.. any of the objects within Vehicle can use that variable, but the Animal Class & all of its objects cannot see or access that variable.
Instance Variables - Instance variables are accessible across methods for each instance of an object. So going back to the Animal Class, I might have a "make_noise" method and maybe I need to create a variable "noise" to store information. I could have a "Dog" object that stores "bark" in the noise variable and a "Cat" object that stores "meow" in the noise variable. So basically I can use the same variable name, but its a unique variable each separate instance of the object and can only be accessed by that object
Local Variables - local variables are only accessible within a single method. So if I had a "Dog" object, he could have different methods such as sleep, eat, walk, etc. If I declare a local variable within the sleep method, eat and walk cannot see that method. To help tie in the explanation, if I instead declared an Instance variable above, the variable could be seen and accessed in each of the eat, walk, and sleep methods.
Didn't meant to turn this into a long definition post, but sometimes I get carried away :) The main point of all of that is that when creating my Array program, I wanted to create an Array class that could create Array objects. I was trying to write my code similar to how it was in the Java code, but for Ruby I needed to create the scope for my variables. I needed an Instance variable that would apply across that object so that in each method I created, it had access to my Array object.
The second "gotcha" or stumbling block for me was equality. In programming, = vs == mean two completely different things. With a single = sign, you are setting something equal to something. For example, a = 5 will set the variable "a" to equal 5. Whereas with two equals sings "==" you are comparing two things. So with "a == 5" you could compare does "a equal 5". If it already does, one thing can happen, but if it doesn't something else might happen. I had a few different areas where I used a single equals sign instead of a double equals sign, and it created odd behavior in my program.
One thing I've found is that when learning to code, you are trying to absorb so much new information that it can be easy to slip up and miss one minor thing, such as a single = sign versus a double == sign. I suppose the bright spot is that I can at least identify and fix my error now rather than having to post to Stackoverflow for someone else to explain it to me!
Well back to coding for a few more hours before I get my weekend started!
Showing posts with label dataStructures. Show all posts
Showing posts with label dataStructures. Show all posts
Saturday, October 25, 2014
Wednesday, October 22, 2014
5am Challenge - Day 1 (officially...)
Even staying up a little later than I typically do, I still managed to force myself out of bed around 5:05 this morning. It's actually not so bad once you actually stand up for a minute or two and force yourself out of the comfort and warmth of bed. I let the dogs out as usual, grabbed a quick cup of coffee, and now time for a quick update before I dive back into my studies.
One thing to mention for those that have been following along... I've heard some great advice from other bloggers (about code) that I thought would be useful and I'm going to follow a similar format. That is, I'm really writing these for me as a refresher, and if I'm able to provide value for other readers great! The basic idea is that I can blog about things I learn, and it keeps a running record of documentation (or notes) on topics and problems I encounter. That way if I run into something similar down the road I can think to myself..."aha..I wrote a blog post about that way back when..." and search back and find my notes.
I'm getting to the point with code where I can start to read and understand, but it takes me longer to actually write or come up with my own code. It's very similar to learning a foreign language, like Spanish for example. I took a few years of Spanish in high school, and while to this day I can read certain things and understand the gist, it is much harder to listen and understand a native Spanish speaker, and even harder for me to form more than a very basic sentence that even a 4 or 5 year old would say.
I'm currently working on Ruby because I'm trying to get the syntax down enough so that I can implement my Data Structures in Ruby as well as Java. Java is a lot more rigid and you have to directly state certain things, whereas in Ruby you can write less code and accomplish the same thing. Ruby reads a lot like pseudo code (very similar to plain English just explaining on how a block of code would work without worrying about exact syntax).
For example, here is one simple line of Ruby that anybody can probably guess what it does:
def greeting(name)
puts "Hello #{name}!"
end
greeting("World")
As you might have guessed, the output of this simple Ruby script would read "Hello World!"
I spent about 2 hours yesterday morning and another 30-45 minutes yesterday evening both working through the CodeAcademy (www.codeacademy.com) Ruby Tutorial as well as updated my first problem I'm trying to implement in Ruby. I at least got the first part working which is really just an Array class to construct my arrays from. I got to the Object Oriented section of the Ruby tutorial which really helped because it also reminded me of Instance variables and what scope I need to provide which variables. I'm pretty sure the reason my initial solution was not working was for this very reason. I was trying to call a variable from a different method that did not have access to that variable. Here is the little bit of code so far, but I should get a lot more of it done today:
One thing to mention for those that have been following along... I've heard some great advice from other bloggers (about code) that I thought would be useful and I'm going to follow a similar format. That is, I'm really writing these for me as a refresher, and if I'm able to provide value for other readers great! The basic idea is that I can blog about things I learn, and it keeps a running record of documentation (or notes) on topics and problems I encounter. That way if I run into something similar down the road I can think to myself..."aha..I wrote a blog post about that way back when..." and search back and find my notes.
I'm getting to the point with code where I can start to read and understand, but it takes me longer to actually write or come up with my own code. It's very similar to learning a foreign language, like Spanish for example. I took a few years of Spanish in high school, and while to this day I can read certain things and understand the gist, it is much harder to listen and understand a native Spanish speaker, and even harder for me to form more than a very basic sentence that even a 4 or 5 year old would say.
I'm currently working on Ruby because I'm trying to get the syntax down enough so that I can implement my Data Structures in Ruby as well as Java. Java is a lot more rigid and you have to directly state certain things, whereas in Ruby you can write less code and accomplish the same thing. Ruby reads a lot like pseudo code (very similar to plain English just explaining on how a block of code would work without worrying about exact syntax).
For example, here is one simple line of Ruby that anybody can probably guess what it does:
def greeting(name)
puts "Hello #{name}!"
end
greeting("World")
As you might have guessed, the output of this simple Ruby script would read "Hello World!"
I spent about 2 hours yesterday morning and another 30-45 minutes yesterday evening both working through the CodeAcademy (www.codeacademy.com) Ruby Tutorial as well as updated my first problem I'm trying to implement in Ruby. I at least got the first part working which is really just an Array class to construct my arrays from. I got to the Object Oriented section of the Ruby tutorial which really helped because it also reminded me of Instance variables and what scope I need to provide which variables. I'm pretty sure the reason my initial solution was not working was for this very reason. I was trying to call a variable from a different method that did not have access to that variable. Here is the little bit of code so far, but I should get a lot more of it done today:
Yes it doesn't look like much for over 2 hours, but it was more studying through CodeAcademy and then refactoring my code to make this first part work, so I'm proud of it! That's it for today...time for some studying!
Labels:
5amChallenge,
coding,
dataStructures,
learning,
Ruby
Location:
Atlanta, GA, USA
Subscribe to:
Comments (Atom)