This time it’s back to the future, or more specifically, back to arrays and considering some of the things that we can do with them now that we have tools like loops to help us work with them.
One of the things that arrays allow us to do is to “push” items onto them and “pop” them off. This hearkens back to C programming for me and using UNIX to “push and pop” items off of a stack. I sort of got what that was all about, but it’s been awhile, so it’s good to get the chance to see how Ruby handles this, and give me a chance to re-explore this approach (and maybe learn it for real this time 🙂 ).
Zed explains that the approach to pushing an item onto an array covers a number of steps.
Let’s use this example: mystuff.push(‘hello’)
1. Ruby looks up the mystuff variable. From there it determines what kind of variable mystuff is (a function call? A global variable?), In this case, Ruby finds it and sees that it is indeed an array.
2. Next, it sees the period (‘.‘). This keys the Ruby interpreter to start looking at possible functions (methods) associated with this variable (in this case, an array).
3. When it sees “push” in the variable name. Ruby checks to see if that is a viable and legitimate function (which it is), and prepares to use it.
4. The ‘( )‘ (parentheses) let the Ruby interpreter know that what follows is a function call of some kind, and that the value inside of the parentheses is any number of arguments, depending on the function being used (in this case, the string ‘hello‘).
5. Finally, when it reaches the end of the variable value, with all of its methods and function calls, it then executes that step in the code.
So yeah, that’s a lot of stuff to consider, and that’s all with just that simple statement.
All right, let’s check out the code for this exercise and see if we understand what’s happening.
And here is what we see:
Go read about “Object Oriented Programming” online. Confused? Yeah I was too. Do not worry. You will learn enough to be dangerous, and you can slowly learn more later. Read up on what a “class” is in Ruby. Do not read about how other languages use the word “class”. That will only mess you up.
[Yep, this is something I’ve had a touch and go understanding of for years, much more touch than go. Generically, I get it. There are objects, and there are ways we can interact with those objects. We can use and reuse objects, we can create whole libraries of simple objects that we can call on as we need them. that much I get. In the Ruby world, a class is a top level object that can have its own methods defined. If we want to make something new in Ruby, we define it as a class, and then we create methods to interact with that class. as long as we name it something that doesn’t conflict with a dedicated Ruby keyword, we’re clear. Classes are also able to have their own scope and their own variable space, like any function. Classes can also create functions and nest those functions. Functions can also be sub-categorized, and those can be subcategorized even more. When we see something like Class.method.sub-method.sub-sub-method(‘argument’), each period shows that we are calling a function that resides within that given function.]
What’s the relationship between something.methods and the “class” of something?
If you do not have any idea what I’m talking about do not worry. Programmers like to feel smart so they invented Object Oriented Programming, named it OOP, and then used it way too much. If you think that’s hard, you should try to use “functional programming”.
[I think I explained that above :). ]
The ability to loop and iterate with arrays, and to have tools that allow us to ad and remove items from an array is very powerful when it comes to processing text. This is how UNIX systems do things like grep and sort and awk, etc. It’s cool to see it in this manner, and with a language that feels less bulky, so that we can get a handle on what is actually happening. the ability to manipulate strings and arrays using functions as part of a class is also pretty cool. I expect I’ll be working quite a bit more with classes over the ensuing few days.