Arrays in Ruby are similar to Lists in Python: highly mutable, can contain literally anything, and can do just about anything you might want. Creating them is quite simple, and can be done in many ways, as this code shows:
Which puts this:
Which makes perfect sense.
Manipulating lists is also quite intuitive, and can be accessed in a myriad of ways. Here are examples of most common methods:
which puts a very, very long response (with apologies for egregious repetition so it's legible):
And now it's time for a very special little method: uniq. You saw it earlier, but that's its non-destructive form. When it's made destructive with an !, then it has a very interesting effect:
recall that monster of an array? Well, it's now:
Quite nice, isn't it?
So, now, bubble sort time!
This is the code, and this:
is what it does. With this array:
it produces this result:
And so it goes.
Anyways, that's arrays finished, and a lovely bubble sort to round it all out.
See you on the next post!
Tuesday, February 28, 2017
Friday, February 17, 2017
Methods, Classes, and Variables
Ruby is an object-oriented language, and thus has very simple class and method support. In addition, it has a wide range of variables which have different properties. Here's an example of a rather simple class:
This particular class has instance methods (methods which pertain to one instance of a class), a class method (akin to a static method in Java), an instance variable (which pertains to one instance of a class), and a class variable (akin to a static variable in Java).
In addition, it references two other variables, one global and one local. Global variables can be accessed anywhere, while local ones are tied to their specific area. Here, they act in similar fashions, but they are fundamentally different.
When you take this class and add some code like this:
Then you can see that it will print a large amount of statements. First, it instantiates a RandomClass with a value of one, prints out the variable, prints out a toString type thing, sets the variable to something different, and then prints out that toString substitutes again.
Then, it prints the local variable, sets the local to the return of the static method, and then prints it again, before it creates a temporary instance with value 20, and then prints it.
After this, it sets the global variable to 18, prints out the value of the static method, creates a new class using the global as the value, and then prints the toString.
Finally, it prints the class variable. The result is this:
This is exactly what you would expect, with the only unique, Ruby-esque twists being the symbols before the variable names to designate their purpose and the "::" instead of the "." most other languages use to call a static (or any kind) of variable.
The three people whose blogs I commented on are:
Julian Kastelic, whose blog is https://perlnecklace.wordpress.com/,
Joseph Pernick, whose blog is http://cs270joepernick.blogspot.com/,
and Caroline Marcus, whose blog is https://swift359.wordpress.com/.
The three people whose blogs I commented on are:
Julian Kastelic, whose blog is https://perlnecklace.wordpress.com/,
Joseph Pernick, whose blog is http://cs270joepernick.blogspot.com/,
and Caroline Marcus, whose blog is https://swift359.wordpress.com/.
Friday, February 10, 2017
Ruby Post -- The Threequel
Ruby is rather versatile, being an embeddable (server side and in object code) language, an extension language, an impure functional language, a hardware description language (digital circuit design), an imperative language, an interactive language, an interpreted language, a metaprogramming language, an object-oriented language (single dispatch), a reflective language, a scripting language, and thus a multiparadigm language.
The reason that there are so many languages (and no perfect one) can perhaps be summed up best by XKCD:
The reason that there are so many languages (and no perfect one) can perhaps be summed up best by XKCD:
It's not dealing with languages, but it explains beautifully how many languages, including Ruby, began. Someone was dissatisfied with the current selection, thought it needed some definitive readjustment, and added to the ~1500 languages which exist today. And there can't be a perfect language for the reason that you have to give to get, where to acquire convenience you have to sacrifice speed and precision (which is why Python is so much slower than C), to become more versatile you have to become less exact (Java vs. Perl), and Basic, despite being able to do just about anything, isn't ever used anymore.
Anyways, onto some actual code.
Ruby has full support for all standard loop types, and has relatively simple syntax. Here's an example of for loops:
Note that two dots make the variable go one step farther, while three produce the more traditional result. The "#{}" thingy is pretty much string concatenation, where it will just put whatever i happens to be there. Note also that the return value is the loop range, with the dots still preserved.
While loops are also quite simple:
Note that there's a "do" at the end of the while line, and that it returns a nil.
Here's an until loop, to show the slight difference:
And a wild Collatz Conjecture appears! Note the lovely nested conditional and the return of nil.
Moving out of a line editor, one finds themselves in the labyrinth of disagreeing sites, all trying to explain why their way is the best. This one, however, managed to get everything working properly: http://railsapps.github.io/installrubyonrails-mac.html, and this one directed me to Atom: https://learnrubythehardway.org/.
I then downloaded Atom at https://atom.io/, and as such a simple file like:
can be run with:
but note the lack of a return.
Friday, February 3, 2017
Post Two: The Basics
Given that Ruby is, in fact, a programming language, it can handle things like math, variables, conditionals, and loops. In Ruby, math is very similar to other sorts of math. You can see some ramblings of what works and doesn't work here:
And there you go. It's pretty intuitive, although I did expect logs to be a little bit different. If you want a particular base, you just do this:
And everything works out well in the end. Variables are also easy, like so:
And conditionals are written like this:
While for loops look like this:
Note that two dots make the variable go one step farther, while three produce the more traditional result. The "#{}" thingy is pretty much string concatenation, where it will just put whatever i happens to be there. Note also that the return value is the loop range, with the dots still preserved.
While loops are also quite simple:
Note that there's a "do" at the end of the while line, and that it returns a nil.
Here's an until loop, to show the slight difference:
And a wild Collatz Conjecture appears! Note the lovely nested conditional and the return of nil.
Next time, I'll get the file system working so that irb(main): stops showing up and I'll be finally able to edit my mistakes!
Anyways, 'twas fun, and see you some time before the twelfth!
Jamie
Subscribe to:
Posts (Atom)