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

regular expressions, Ruby, ruby lesson, ruby уроки, string, user guide, руководство пользователя

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.
Support the author Donate

Comments

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

Hello, Dear Users of EVILEG!!!

If the site helped you, then support the development of the site financially, please.

You can do it by following ways:

Thank you, Evgenii Legotckoi

A
April 6, 2020, 5:24 a.m.
Alexey

C++ - Test 001. The first program and data types

  • Result:86points,
  • Rating points6
R
April 5, 2020, 2:32 p.m.
Roman

C++ - Test 002. Constants

  • Result:91points,
  • Rating points8
AP
April 5, 2020, 2:31 p.m.
Alexander Peshkov

C++ - Test 001. The first program and data types

  • Result:60points,
  • Rating points-1
Last comments
April 3, 2020, 8:06 a.m.
Konstantin Grudnitskiy

Я надеюсь вы уже разобрались в чем дело, но если вдруг нет, то проблема состоит в том, что вы пытаетесь запустить программу из интерпретатора питона. Файл main.py это уже готова…
April 3, 2020, 6:18 a.m.
Konstantin Grudnitskiy

>>> text = 'hello world'>>> ' '.join(word for word in text.split()[:-1])'hello'>>> def remove_last_word(text):... return text and ' '.join(word for word in text.s…
March 27, 2020, 2:40 p.m.
Evgenij Legotskoj

Добрый день. В конце пятой статьи скачать можете.
March 27, 2020, 2:28 p.m.
mkdir _

Здравствуйте, а можно, пожалуйста, ссылку на целые исходники, если есть?
March 27, 2020, 4:36 a.m.
Evgenij Legotskoj

Скорее всего также, как и для установки всех остальных переменых в CMake, через использование set
Now discuss on the forum
April 6, 2020, 4:19 a.m.
Evgenij Legotskoj

Попробуй не переустанавливать материал, а просто менять цвет
April 6, 2020, 3:39 a.m.
Evgenij Legotskoj

Добрый день. Это всё работает, вот только если есть переопределения каких-то методов, то нет гарантии, что что-то не сломается. Думаю, что нужно допиливать стилевоё оформление в прокси…
April 5, 2020, 11:38 a.m.
Mihailll

Так работает windeployqt --qmldir E:\QTProject\testQmlAndCpp E:\test\build-testQmlAndCpp-Desktop_Qt_5_14_1_MinGW_32_bit-Release
April 5, 2020, 2:35 a.m.
Mihailll

Так работает console.log(textEmail.text) var str = textEmail.text; var n = str.search(/^((([0-9A-Za-z]{1}[-0-9A-z\.]{1,}[0-9A-Za-z]{1})|([0-9А-Яа-я]{1}[-0-9А-я\.]{1,}[…
April 3, 2020, 8:48 a.m.
Intruder

Евгений, добрый день. Спасибо!
EVILEG
About
Services
© EVILEG 2015-2019
Recommend hosting TIMEWEB