I bought it three months ago, but never read it once since then. It is a Chinese-translated edition, because the original book is written in Japanese, which I am totally not familiar with, not even a word. I gonna to read it, write some notes and hope finish this book in not very long time.
Basics
“Hello World” is always a best friend of novice.
I am going to begin my journey of ruby with its basic output functions.
Standard outputs
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Standard outputs revisited
1 2 3 4 5 6 7 |
|
Loops
1 2 3 4 5 6 |
|
What?
Yes, that’s a use of chain rule, as times
is a function that return itself, which is 10.
10 is a instance of Fixnum
, which is also a subclass of Integer
.
Let’s take look at the big picture of Numeric
class hiearchy in Ruby.1
1 2 3 4 |
|
Everything is object
That’s ruby, everything is object. Unlike java, there is no primitive type.
You might wondering where print()
comes from2.
IO
is where it comes from. There are three constant, STDIN, STDOUT and STDERR, which is pointed by
ruby’s global variables, $stdin, $stdout and $sterr respectively.
print()
is a alias of $stdout.print()
provided by ruby’s Kernel
module.
1 2 3 |
|
Array and Hash
Beside String
and Numeric
two basic structure, Ruby has two more advanced data structure -
Array
and Hash
.
You can think of both as Java’s List
and Map
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 2 3 4 5 6 7 8 |
|
:city
is a Symbol
of ruby, you can think of this as a lightweight
version of String
.
The same Symbol object will be created for a given name or string for the duration of a program's execution, regardless of the context or meaning of that name.
Per doc says, there will be one instance of Symbol with same name till program ended.
Notes
For more information, see Ruby Reference ↩