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:


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

Tuesday, January 31, 2017

Post One: It Begins

Given that I use a Mac, Ruby is included at shipping.  To access it, simply open Terminal and type "irb", without the quotes.

Ruby is an object-oriented, general-purpose language, which was made by Yukihiro Matsumoto in 1993.  Matsumoto was dissatisfied with the object-oriented languages of the day, which he either considered flawed, like Perl, or not true object-oriented languages, like Python.

I learned about Ruby some time ago, but I did not realize its utility until the Health IT conference/lecture/thing, where many of the professionals who spoke said they used Ruby.  After this, I went to Wikipedia and then Ruby-Lang.org, which told me everything I wanted to know.

Anyways, at first glance, Ruby seems quite like Python in structure.  The first Terminal shell reacts in much the same way, although there are some immediate differences such as the syntax and the fact that the values are also returned -- and things like "puts()" (the same as "print()" in Python and "System.out.println()" in Java) return not only the printed line but also a "=> nil".

And as such, here's the famous "Hello World" in Ruby form:
The first command does not print "Hello World", but merely returns the value.  The purpose was to demonstrate what a return looks like.  The second command prints "Hello World" -- notice the lack of quotation marks and the return of nil.

So onward into the mystical world of Ruby, where I shall be uploading both on a rigid schedule and whenever the mood strikes me!

See you sometime before the fifth,

Jamie