Adventure of Ruby

Embark on a thrilling adventure of Ruby with Little A and Little C (and another person)!

Journey.start_with(objects)

Little A and Little C want to create a programming language. They like Both Ruby and English, so they decide that the language will be called Ruby.

They say it should be as much like the real life as possible, so in Ruby, everything is an item, or let's call it object.

Almost everything in Ruby is an object.

Little A doesn't know what self is supposed to mean. Little C says, "Let's say it represents the object you're operating on right now!" So the following code represents main, namely the main function:

self

So far, it can only represent objects, and Little C is not happy with that, because they still need to manipulate an object. They decide to use something they call method to interact with it. An object's method is its internal property, so they decided to use . to call an object's "internal" methods.

"What's 1's next number?"

"2, of course."

"In Ruby, we do the same!"

1.next

The code above represents 2.

Then how to check if a number is even? Little A says it would be 1.next.even, but Little C doesn't think it clear enough. By adding ?, he makes it much clearer.

1.next.even? # "?" represents a question

Objects are friendly! They are happy to tell you what methods they provide by simply calling the methods method on them.

1.methods

And methods is a method too, so Little A does this:

1.methods.methods

Little A and Little C are satisfied. But a new question comes: how to do math? Following the same recipe, we get this:

1.+(2)

Too weird. Thanks to Little A who makes something special, you could simply code like this:

1 + 2 # this is same as 1.+(2) 

Ruby makes an exception in its syntactic rules for commonly used operators so you don't have to use periods to invoke them on objects.

puts '#{name} loves Ruby.'

Now they want to make a brief self-introduction in Ruby. Something must be used to restore thier names, which is called a variable. And also, their names are just strings. Strings are usually like'...' or "...".

Example:

name = 'Little C' # we use "a = b" to assign b to a

They want to output it. Here's the solution.

name = 'Little C'
puts name

A piece of cake, itsn't it? But what if Little A wants to add some additional information? Liitle C helps him figure out the problem by using string interpolation.

name = 'Little C'
friend = 'Little A'
puts 'My name is #{name}.'
puts 'I helped #{friend}.'

Note that there is a line break after each output when puts is used. And they create more operations about strings... Let's take a look.

  • a.include?(b) checks if string aa contains string bb.
  • a.start_with?(b) checks if aa starts with bb.
  • a.end_with?(b) checks if aa ends with bb.
  • a.index(b) gets the earliest occurrence of bb in aa. It is empty if it does not exist.
  • a.upcase gets the result of converting aa to uppercase.
  • a.swapcase gets the result of swapping the case of aa.
  • a.split(b) splits aa by bb.
  • a + b & a.concat(b) aa concatenates bb, creating a new string, which is less efficient.
  • a << b aa concatenates bb, adding bb directly after aa.
  • a.sub(b, c) Replaces the first bb in aa with cc.
  • a.sub(regex, b) Same as above, match replaced by regular expression.
  • a.gsub(b, c) Replaces all bb's in aa with cc (global).
  • a.match(regex) Finds the first string in aa that matches the regex.
  • a.match(regex, pos) Finds the first string in aa that matches the regex, starting at the pospos position.

A regular expression sometimes referred to as rational expression, is a sequence of characters that specifies a match pattern in text.

That explains how much they like English.

You_enjoy_yourself == true

Little A forgets the value of the varible namename. He wants to check if it equals 'Little A'. In this case, Little A uses ==.

name = 'Little C'
puts name == 'Little A'

Obviously, the output is false. If we change A into C, then we can get true.

The other usual operators like greater than (>), less than (<), greater than or equal to (>=) etc. are supported.

Little A isn't smart enough, he wants to combine the boolean expressions but doesn't know how to do with it. Little C helps him by using && and ||.

A && B == true holds when A and B are both true. A || B == true holds when one or more of A and B is true. !A is false when A is true; !A is true when A is false.

If you like me, I like you; otherwise, I don't like you

Little A finds he can't help falling love with Little C, so he asks Little C whether Little C loves him. That's how Little C responds.

if you_like_me
	I_like_you = true
else
	I_like_you = false
end

Of course, the code fails to run since varible you_like_meyou\_like\_me is undefined. That depends on Little A's attitude towards Little C then! But let's now talk about the if-else construct:

if (conditional-expression)  
  # code if condition is true  
else   
  # code if condition is false  
end 

"How about this?" Little A codes:

how_much_I_like_you = 100
if (how_much_I_like_you == 100)
	puts "I like you to my bones."
elsif (how_much_I_like_you >= 60)
	puts "I kinda like you."
else
	puts "I'm not that into you."
end

Little C changes how_much_I_like_you = 100 into how_much_I_like_you = 114514. Both of them laugh. The if-elsif-else construct:

if (condition-expression1)   
  # code if above condition is true  
elsif (condition-expression2)  
  # code if above condition is true  
elsif (condition-expression3)   
  # code if above condition is true  
...  
else   
  # code if all the conditions are false  
end  

It's easy unless you have learnt about "unless" construct! That's actually simple.

unless you_like_me # = if not
	puts "I don't like you, either!"
end

Fun facts:  the objects false and nil equates to false. Every other object like say 1, 0, "" are all evaluated to be true.

if 0
  puts "Hey, 0 is considered to be a truth in Ruby" 
end

loop do puts "I love you" end