Evgenii Legotckoi
Evgenii LegotckoiJune 23, 2016, 2:01 a.m.

User Guide #07 - Ruby - Back to the simple examples

Now let's take apart the code of some of our previous example programs.

The following appeared in the simple examples chapter.

def fact(n)
  if n == 0
    1
  else
    n * fact(n-1)
  end
end
print fact(ARGV[0].to_i), "\n"

Because this is the first explanation, we examine each line individually.


Factorials

def fact(n)

In the first line, def is a statement to define a function (or, more precisely, a method ; we'll talk more about what a method is in a later chapter). Here, it specifies that the function fact takes a single argument, referred to as n .

if n == 0

The if is for checking a condition. When the condition holds, the next bit of code is evaluated; otherwise whatever follows the else is evaluated.

1

The value of if is 1 if the condition holds.

else

If the condition does not hold, the code from here to end is evaluated.

n * fact(n-1)

If the condition is not satisfied, the value of if is the result of

n
times fact(n-1).

end

The first end closes the if statement.

end

The second end closes the def statement.

print fact(ARGV[0].to_i), "\n"

This invokes our fact() function using a value specified from the command line, and prints the result.

ARGV is an array which contains command line arguments. The members of ARGV are strings, so we must convert this into a integral number by to_i. Ruby does not convert strings into integers automatically like perl does.

Strings

Next we examine the puzzle program from the chapter on strings . As this is somewhat longer, we number the lines for reference.

words = ['foobar', 'baz', 'quux']
secret = words[rand(3)]

print "guess? "
while guess = STDIN.gets
  guess.chop!
  if guess == secret
    print "you win\n"
    break
  else
    print "you lose.\n"
  end
  print "guess? "
end
print "the word is ", secret, ".\n"

In this program, a new control structure, while , is used. The code between while and its corresponding end will execute repeatedly as long as some specified condition remains true.

rand(3) in line 2 returns a random number in the range 0 to 2. This random number is used to extract one of the members of the array words .

In line 5 we read one line from standard input by the method STDIN.gets . If EOF (end of file) occurs while getting the line, gets returns nil . So the code associated with this while will repeat until it sees D* (or * Z under DOS), signifying the end of input.

guess.chop! in line 6 removes the last character from guess ; in this case it will always be a newline character.

In line 15 we print the secret word. We have written this as a print statement with three arguments (which are printed one after the other), but it would have been equally effective to do it with a single argument, writing secret as #{secret} to make it clear that it is a variable to be evaluated, not a literal word to be printed:

print "the word is #{secret}.\n"

Regular expressions

Finally we examine this program from the chapter on regular expressions .

st = "\033[7m"
en = "\033[m"

while TRUE
  print "str> "
  STDOUT.flush
  str = gets
  break if not str
  str.chop!
  print "pat> "
  STDOUT.flush
  re = gets
  break if not re
  re.chop!
  str.gsub! re, "#{st}\\&#{en}"
  print str, "\n"
end
print "\n"

In line 4, the condition for while is hardwired to true , so it forms what looks like an infinite loop. However we put break statements in the 8th and 13th lines to escape the loop. These two breaks are also an example of if modifiers. An " if modifier" executes the statement on its left hand side if and only if the specified condition is satisfied.

There is more to say about chop! (see lines 9 and 14). In ruby, we conventionally attach '!' or '?' to the end of certain method names. The exclamation point (!, sometimes pronounced aloud as "bang!") indicates something potentially destructive, that is to say, something that can change the value of what it touches. chop! affects a string directly, but chop with no exclamation point works on a copy. Here is an illustration of the difference.

ruby> s1 = "forth"
  "forth"
ruby> s1.chop!       # This changes s1.
  "fort"
ruby> s2 = s1.chop   # This puts a changed copy in s2,
  "for"
ruby> s1             # ... without disturbing s1.
  "fort"

You will later come across method names that end in a question mark ( ? , sometimes pronounced aloud as "huh?"); this indicates a "predicate" method, one that can return either true of false.

Line 15 deserves some careful attention. First, notice that gsub is another so-called destructive method. It changes str by replacing everything matching the pattern re ( sub means substitute, and the leading

g
means global, i.e., replace all matching parts in the string, not just the first one found). So far, so good; but what are we replacing the matched parts of the text with? st and en were defined in lines 1-2 as the ANSI sequences that make text color-inverted and normal, respectively. In line 15 they are enclosed in #{} to ensure that they are actually interpreted as such (and we do not see the variable names printed instead). Between these we see "\&" . This is a little tricky. Since the replacement string is in double quotes, the pair of backslashes will be interpreted as a single backslash; what gsub! actually sees will be "\&" , and that happens to be a special code that refers to whatever matched the pattern in the first place. So the new string, when printed, looks just like the old one, except that the parts that matched the given pattern are highlighted in inverse video.

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
AD

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:20points,
  • Rating points-10
Last comments
ИМ
Игорь МаксимовNov. 22, 2024, 10:51 p.m.
Django - Tutorial 017. Customize the login page to Django Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
Evgenii Legotckoi
Evgenii LegotckoiNov. 1, 2024, 12:37 a.m.
Django - Lesson 064. How to write a Python Markdown extension Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
A
ALO1ZEOct. 19, 2024, 6:19 p.m.
Fb3 file reader on Qt Creator Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html
ИМ
Игорь МаксимовOct. 5, 2024, 5:51 p.m.
Django - Lesson 064. How to write a Python Markdown extension Приветствую Евгений! У меня вопрос. Можно ли вставлять свои классы в разметку редактора markdown? Допустим имея стандартную разметку: <ul> <li></li> <li></l…
d
dblas5July 5, 2024, 9:02 p.m.
QML - Lesson 016. SQLite database and the working with it in QML Qt Здравствуйте, возникает такая проблема (я новичок): ApplicationWindow неизвестный элемент. (М300) для TextField и Button аналогично. Могу предположить, что из-за более новой верси…
Now discuss on the forum
m
moogoNov. 22, 2024, 6:17 p.m.
Mosquito Spray System Effective Mosquito Systems for Backyard | Eco-Friendly Misting Control Device & Repellent Spray - Moogo ; Upgrade your backyard with our mosquito-repellent device! Our misters conce…
Evgenii Legotckoi
Evgenii LegotckoiJune 25, 2024, 1:11 a.m.
добавить qlineseries в функции Я тут. Работы оень много. Отправил его в бан.
t
tonypeachey1Nov. 15, 2024, 5:04 p.m.
google domain [url=https://google.com/]domain[/url] domain [http://www.example.com link title]
NSProject
NSProjectJune 4, 2022, 1:49 p.m.
Всё ещё разбираюсь с кешем. В следствии прочтения данной статьи. Я принял для себя решение сделать кеширование свойств менеджера модели LikeDislike. И так как установка evileg_core для меня не была возможна, ибо он писался…

Follow us in social networks