Ruby | 1 basic grammar

◇ the Ruby
Ruby is a programming language for creating a "system" of WEB applications.
As a language, such as similar, there are things like PHP and Python.

◇ Hello World

# 语法
puts "输出文字"

# 实例
puts "Hello World"

◇ instruction
one of puts the "instructions", meaning "you should output the value of after the puts to the console"
after the puts, you must open the "spaces".

◇ How to write comments
and write a # at the beginning of the line, which makes the line a comment.


◇ string
string must be enclosed in quotation marks ( 'or ").

◇ string concatenation
and to a string to each other using the "+" symbol, you can concatenate strings with each other.

# 语法
puts "AA" + "BB"
⇒输出 AABB

# 用例
puts "小猫" + "小狗"
⇒输出 小猫小狗

◇ the numerical
values are not enclosed in quotation marks.
Between numbers and symbols you must also open a space.

◇ calculation of numerical
codes *读作asterisk, 9% 2 exported manner Shi 9 de than 2 target excess number

◇ of string and numeric difference

# 数值
puts 5 + 2
⇒输出 7

# 文字列
puts "5 + 2"
⇒输出 5 + 2
puts "5" + "2"
⇒输出 52

variable

# 变数的概念
变数是一个箱子,变数名是贴在箱子上的标签,值是箱子里装的东西

# 定义变数的语法
変数名 = 値
※ 此处等号的意思是【代入】

# 用例
name = "John"

# 变数的用法
puts name
注意:变数不需要引号,否则会被视为文字列

◇ location can use a variable
to use the variable, you can not unless after sure to define the variable.

◇ variable containing the string
to the value obtained by substituting and call the variable "replaces" so variable that contains the string can be treated in exactly the string the same way.
Rei如:

message = "你好"
puts message + "午安"
⇒输出 你好午安
name = "小猫"
puts message + name
⇒输出 你好小猫

Similarly, if the number is entered variables, it can be used the same way as a numerical value.

◇ Why use a variable

  • Use repeating the same elements
  • Easy to respond to changes
  • Cheap what elements Kawakari
# 不使用变数时
puts "A喜欢Ruby"
puts "B喜欢Ruby"
puts "C喜欢Ruby"
 ⇒ 要将Ruby换成Python时,就需要修改3处

# 使用变数时
program = Ruby
puts "A喜欢" + program
puts "B喜欢" + program
puts "C喜欢" + program 
⇒ 要将Ruby换成Python时,只需修改1处

◇ naming Rule
Let's put the variable name appropriate to the value.
Variable name can be determined freely, but there are several in Ruby naming rules.
In particular, when you put the variable name which is a combination of two or more words, let's remember that the use of the underscore.

◇ of variable update

  1. Variable, you can also change the once assignment value.
    Once the variables that you assign a value, and then again to assign a value, the contents of the variable in the assignment value to the later will be overwritten.
number = 10
puts number
⇒ 输出10
number = 20
puts number
⇒ 输出20
  1. To assign to yourself
number = 10
puts number
⇒ 输出10
number = number + 5
puts number
⇒ 输出15

It omitted the writing

The basic shape Abbreviation
x = x + 10 x += 10
x = x - 10 x -= 10
x = x * 10 x *= 10
x = x / 10 x /= 10
x = x % 10 x %= 10

◇ variable expansion
is to include the value of the variable in the string.
This is called "variable expansion".

# 语法
#{变数名}

# 用例
name = "John"
puts "你好#{name}"
⇒ 输出 你好John

When the variable deployment there is a caveat.
In fact, it will not be only variable expansion case of strings with double quotes.
In the case of single quotes, not performed variable expansion, let's note that the result is output as it is as a string.

puts '你好#{name}'
⇒ 输出 你好#{name}

The benefits of variable expansion
can not be consolidated in addition the numbers and strings.
However, when using the variable expansion, variable containing a numeric value can also be linked to a string without any problems.

age = 19
puts age + "岁"
⇒ 输出error
puts "#{age}岁"
⇒ 输出 19岁

if statement: conditional branch

# 语法
if 条件式
    处理
end

# 用例
score = 94
if score > 80
    puts "做得很好"
end

When the condition is not satisfied, if the process is not executed.

◇ boolean and compare operator

  1. Conditional expression of output
score = 94
puts score > 80
⇒ 输出 true
symbol meaning
a == b Equal to true when a and b
a != b a and b are unequal to true

◇ else: the process of when the condition is not satisfied

# 语法
if 条件式
    处理(当条件式为true的时候执行)
else
    处理(当条件式为false的时候执行)
end

# 用例
score = 100
if score == 100
    puts "满分"
else
    puts "不是满分"
end

◇ indent
and indent means "indentation" in Japanese.
Pressing the tab key once, you can single-byte space two minute indent.

◇ elsif: To add a condition

# 语法
if 条件1
    处理(条件1为true时执行)
elsif 条件2
    处理(条件1为False,且条件2为true时执行)
else
    处理(条件1&2皆为false时执行)
end

# 用例
score = 77
if score > 80
    puts "优秀"
elsif score > 60
    puts "合格"
else
    pus "不合格"
end

Notes: Only the processing of the first on the condition that matches being executed in elsif. The wrong order of the order conditions, we may expect the branch is not made.

◇ to combine the conditions

symbol meaning
&& And
II Or
# 用例
score = 68
if score <= 80 && score > 60
    puts "及格"
end

Exercise

Let's challenge to the overall assignments.
If you assign a favorite year in a variable, you create a program that outputs whether the Summer Olympics were held in that year.

# 好きな年を西暦(例:2000)で代入してください
year = 3927

# 以下の条件分岐に、新しい条件を追加してください
if year < 1896
  puts "#{year}年にオリンピックはまだありません"
elsif year == 1916 || year == 1940 || year == 1944
  puts "#{year}年にオリンピックは開催されませんでした"  
elsif year % 4 == 0
  puts "#{year}年は夏季オリンピックが開催されました"
else 
  puts "#{year}年は夏季オリンピックイヤーではありません"
end

Guess you like

Origin blog.csdn.net/weixin_33796177/article/details/90778412