Loading

:D 获取中...

Adventure of Ruby

Character set: - **Little A** hard working and caring, sometimes rely too much on books - **Little B**

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 Little C gets the idea 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 the number is even? Little A says it must be 1.next.even, but Little C doesn't think it's clear enough. By adding ?, he makes it much clearer.

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

Friendly, objects are happy to tell you what methods they provide by simply calling the methods method on them.

1.methods

"Is methods a method as well?" asks Little C.

"Yeahhh." 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, they get this:

1.+(2)

Too weird. Thanks to Little C 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 \(a\) contains string \(b\).
  • a.start_with?(b) checks if \(a\) starts with \(b\).
  • a.end_with?(b) checks if \(a\) ends with \(b\).
  • a.index(b) gets the earliest occurrence of \(b\) in \(a\). It is empty if it does not exist.
  • a.upcase gets the result of converting \(a\) to uppercase.
  • a.swapcase gets the result of swapping the case of \(a\).
  • a.split(b) splits \(a\) by \(b\).
  • a + b & a.concat(b) \(a\) concatenates \(b\), creating a new string, which is less efficient.
  • a << b \(a\) concatenates \(b\), adding \(b\) directly after \(a\).
  • a.sub(b, c) Replaces the first \(b\) in \(a\) with \(c\).
  • a.sub(regex, b) Same as above, match replaced by regular expression.
  • a.gsub(b, c) Replaces all \(b\)'s in \(a\) with \(c\) (global).
  • a.match(regex) Finds the first string in \(a\) that matches the regex.
  • a.match(regex, pos) Finds the first string in \(a\) that matches the regex, starting at the \(pos\) 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 \(name\). 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\_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

To express his love for Little C, Little A decides to print a lot of "I love you!". Instead of printing it line by line, how to do it more effiently? We can

posted @ 2024-06-28 10:19  liuzimingc  阅读(12)  评论(0编辑  收藏  举报