I just gave myself a challenge to master 2 new programming languages by the end of this year 2010. In a nutshell that’s a holidays season gift to myself and my business: knowledge is power. Anyway I just “cracked open” the PDF tutorial I downloaded online. Yes, I read more e-books these days then their hard copy counterparts. It’s more convenient and instantaneous.
So here I am on chapter 1 dealing with the general syntaxes, getting from a keyboard input and putting out that info on a screen. I just need three function “gets()” for the input, “print” and “puts” from the output.
The only difference between print (“….”) and puts(“…”) is the latter adds a linefeed at the end of the output while the former doesn’t.
Coming from languages such as C++, C#, Javascript and PHP I always expect a semi column (;) at the end of each statement. That’ not the case with Ruby.
Declaring a typical variable – a string or a number – is straightforward in Ruby. Not need to specify the variable data type like in C++ or prepend the dollar sign ($) like in PHP or PERL.
Embedding a variable or an expression in a string is somewhat interesting. Let’s assume you have variable:
firstname=”John”
To embed that variable – even an expression – to the displayed result you would write:
print(“My name is #{firstname}”)
It’s not that different from PHP except the use of the # sign preceding the variable or expression to evaluate.
Of course the expression above will replace the embedded variable by its actual value “John” only when the string to be printed (on the screen) is between double quotes. The result will be different is we use single quotes instead.
Now a mistake I will be making for sure is always “prepending” the $ to a variable name. The dollar $ in Ruby is reserved for global variables. meaning those variable not limited with a function or block.
I had a brief overview of classes and objects: instance variables, functions, the initialize and inspect methods. Usually those concepts come up several chapter later in most tutorials. However the author of this Ruby tutorial certainly decided to peak my interest from the get go and now I need to learn more about how Ruby tackles the Object Oriented Programming concept.
I have to say after finishing this first chapter I am a little but uneasy about the new syntaxes but also intrigued enough to keep me going to the next chapter.