日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

ruby 三目运算符_Ruby运算符

發(fā)布時(shí)間:2025/3/11 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby 三目运算符_Ruby运算符 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ruby 三目運(yùn)算符

Ruby運(yùn)算符 (Ruby operators)

Operators are the symbols which assist compiler or interpreter to carry out certain mathematical, logical and relational tasks and produce the results. Operators are method calls with parameters.

運(yùn)算符是幫助編譯器或解釋器執(zhí)行某些數(shù)學(xué),邏輯和關(guān)系任務(wù)并產(chǎn)生結(jié)果的符號(hào)。 運(yùn)算符是帶有參數(shù)的方法調(diào)用。

Ruby運(yùn)算符的類型 (Types of Ruby operators)

Ruby supports a variety of operators but following are few of them:

Ruby支持多種運(yùn)算符,但其中很少有:

  • Arithmetic operators

    算術(shù)運(yùn)算符

  • Comparison operators

    比較運(yùn)算符

  • Assignment operators

    賦值運(yùn)算符

  • Bitwise operators

    按位運(yùn)算符

  • 1)算術(shù)運(yùn)算符 (1) Arithmetic Operators)

    They are mathematical methods that takes two parameters and perform arithmetic operations on them. Following is the list of arithmetic operators,

    它們是采用兩個(gè)參數(shù)并對(duì)它們執(zhí)行算術(shù)運(yùn)算的數(shù)學(xué)方法。 以下是算術(shù)運(yùn)算符列表,

    SymbolNameDescription
    +AdditionIt Adds two provided values
    -SubtractionIt Subtracts two given values
    /DivisionUsed to Divide left hand operand by right hand operand
    *MultiplicationUsed to multiply operands present on both the sides.
    %ModulusGives remainder as result by dividing left hand side operand with the right hand side operand.
    **ExponentIt is used to carry out power calculations on operands.
    符號(hào) 名稱 描述
    + 加成 它添加兩個(gè)提供的值
    -- 減法 將兩個(gè)給定值相減
    / 用于將左操作數(shù)除以右操作數(shù)
    * 乘法 用于將兩側(cè)的操作數(shù)相乘。
    模量 通過(guò)將左側(cè)操作數(shù)除以右側(cè)操作數(shù)得到余數(shù)。
    ** 指數(shù) 用于對(duì)操作數(shù)進(jìn)行冪計(jì)算。

    Example:

    例:

    puts "Enter first value" num1 = gets.chomp.to_i puts "Enter second value" num2 = gets.chomp.to_iadd=num1+num2 puts "Sum is #{add}"sub=num1-num2 puts "Difference is #{sub}"mul=num1*num2 puts "Product is #{mul}"div=num1/num2 puts "Division is #{div}"mod=num1%num2 puts "Remainder is #{mod}"

    Output

    輸出量

    Enter first value 10 Enter second value 3 Sum is 13 Difference is 7 Product is 30 Division is 3 Remainder is 1

    2)比較運(yùn)算符 (2) Comparison Operator)

    They are a variant of binary operators which takes two operands and carry out comparison between them.

    它們是二進(jìn)制運(yùn)算符的變體,它接受兩個(gè)操作數(shù)并在它們之間進(jìn)行比較。

    Following is the list of comparison operators,

    以下是比較運(yùn)算符的列表,

    SymbolDescription
    ==It is used to check whether the two specified operands are equal or not. If yes, then condition becomes true.
    !=When there is a need to check whether the values of operands are not equal to each other, then this operator is used.
    >Used to check if the left hand side value is greater than right hand side operand, if yes then the condition becomes true.
    <Used to check if the right hand side value is greater than left hand side operand, if yes then the condition becomes true.
    >=Used to check if the left hand side value is greater than or equal to right hand side operand, if yes then the condition becomes true.
    <=Used to check if the right hand side value is greater than or equal to left hand side operand, if yes then the condition becomes true.
    <=>It returns 0 if first value is equal second value, 1 if first operand is greater than the second operand and -1 if first operand is less than the later one.
    .eql?It returns true if the receiver and the arguments are of same data type and have equal values.
    .equal?It returns true if the receiver and the arguments hail from same object id.
    符號(hào) 描述
    == 用于檢查兩個(gè)指定的操作數(shù)是否相等。 如果是,則條件變?yōu)檎妗?
    != 當(dāng)需要檢查操作數(shù)的值是否彼此不相等時(shí),將使用此運(yùn)算符。
    > 用于檢查左側(cè)值是否大于右側(cè)操作數(shù),如果是,則條件變?yōu)閠rue。
    < 用于檢查右側(cè)值是否大于左側(cè)操作數(shù),如果是,則條件為真。
    > = 用于檢查左側(cè)值是否大于或等于右側(cè)操作數(shù),如果是,則條件變?yōu)閠rue。
    <= 用于檢查右側(cè)值是否大于或等于左側(cè)操作數(shù),如果是,則條件變?yōu)閠rue。
    <=> 如果第一個(gè)值等于第二個(gè)值,則返回0;如果第一個(gè)操作數(shù)大于第二個(gè)操作數(shù),則返回1;如果第一個(gè)操作數(shù)小于第二個(gè)操作數(shù),則返回-1。
    .eql? 如果接收方和參數(shù)的數(shù)據(jù)類型相同且值相等,則返回true。
    。等于? 如果接收方和參數(shù)來(lái)自相同的對(duì)象ID,則返回true。

    Example:

    例:

    puts "Enter first value" num1 = gets.chomp.to_i puts "Enter second value" num2 = gets.chomp.to_iif num1==num2puts "Both are equal" elsif num1>num2puts "num1 is greater than num2" elsif num1<num2puts "num2 is greater than num1" elsif num1!=num2puts "num2 is not equal to num1" elsif num1>=num2puts "num1 is greater than or equals to num2" elsif num1<=num2puts "num2 is greater than or equals to num1" end

    Output

    輸出量

    Enter first value 100 Enter second value 231 num2 is greater than num1 .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    3)賦值運(yùn)算符 (3) Assignment operators)

    Assignment operators are used to assign the value of right hand side operator to the left hand side operator.

    賦值運(yùn)算符用于將右側(cè)運(yùn)算符的值分配給左側(cè)運(yùn)算符。

    Following is the list of assignment operators which are supported in Ruby,

    以下是Ruby支持的賦值運(yùn)算符的列表,

    OperatorDescription
    =Used to assign right hand value to the left hand operand.
    +=Adds left hand operand with the right hand operand and stores the result in left hand operand.
    -=Subtracts right hand operand from the left hand operand and stores the result in left hand operand.
    *=Multiplies left hand operand with the right hand operand and stores the product in left hand operand.
    %=Used to find modulus by dividing and stores remainder in left hand operand.
    /=Used to find division and stores result in left hand operand
    **=Used to find exponential power and keeps the result in the left operand after computation.
    操作員 描述
    = 用于將右手值分配給左手操作數(shù)。
    + = 將左操作數(shù)與右操作數(shù)相加,并將結(jié)果存儲(chǔ)在左操作數(shù)中。
    -= 從左側(cè)操作數(shù)中減去右側(cè)操作數(shù),并將結(jié)果存儲(chǔ)在左側(cè)操作數(shù)中。
    * = 將左操作數(shù)與右操作數(shù)相乘,并將乘積存儲(chǔ)在左操作數(shù)中。
    %= 用于通過(guò)除法求模并將余數(shù)存儲(chǔ)在左操作數(shù)中。
    / = 用于查找除法并將結(jié)果存儲(chǔ)在左操作數(shù)中
    ** = 用于查找指數(shù)冪并在計(jì)算后將結(jié)果保留在左操作數(shù)中。

    Example:

    例:

    puts "Enter first value" num1 = gets.chomp.to_i puts "Enter second value" num2 = gets.chomp.to_iputs "Enter choice:" ch = gets.chompcase ch when 'a'num1+=1puts "#{num1}" when 'b'num2-=2puts "#{num2}" when 'c'num1*=2puts "#{num1}" when 'd'num1**=9puts "#{num1}" elseputs "option not available." end

    Output

    輸出量

    Enter first value 10 Enter second value 2 Enter choice: c 20

    4)按位運(yùn)算符 (4) Bitwise operators)

    They are used to perform bitwise operations on binary number which includes change of particular bit.

    它們用于對(duì)二進(jìn)制數(shù)執(zhí)行按位運(yùn)算,其中包括更改特定位。

    Following is the list of bitwise operators which are supported by Ruby,

    以下是Ruby支持的按位運(yùn)算符列表,

    SymbolNameDescription
    &Binary ANDIt works by copying a bit to the result if it is present in both the operands
    |Binary ORIt works by copying a bit to the result if it is present in one of the operands.
    ^Binary XORIt works by copying a bit to the result if it is present in one of the operand but not in both.
    ~Binary 1's complementIt flips the bits like making 1 to 0 and vice versa.
    >>Binary left shiftIt shifts the bit to the left by number of bits told by the left operand.
    <<Binary Right shiftIt shifts the bit to the right by number of bits told by the right operand.
    符號(hào) 名稱 描述
    二進(jìn)制與 如果兩個(gè)操作數(shù)中都存在一個(gè)位,則將其復(fù)制到結(jié)果中
    | 二進(jìn)制或 它通過(guò)將一個(gè)位復(fù)制到結(jié)果(如果其中一個(gè)操作數(shù)中存在)來(lái)工作。
    ^ 二進(jìn)制異或 它通過(guò)將一個(gè)位復(fù)制到結(jié)果(如果在一個(gè)操作數(shù)中但在兩個(gè)操作數(shù)中都不存在)來(lái)工作。
    ? 二進(jìn)制1的補(bǔ)碼 它會(huì)將位翻轉(zhuǎn)為1到0,反之亦然。
    >> 二進(jìn)制左移 它將左邊操作數(shù)告知左邊操作數(shù)的位數(shù)。
    << 二進(jìn)制右移 它將右邊的操作數(shù)告知右邊的位數(shù)。

    Example:

    例:

    puts "Enter first value" num1 = gets.chomp.to_i puts "Enter second value" num2 = gets.chomp.to_ia1=num1 & num2 puts "and is #{a1}"a2=num1 | num2 puts "OR is #{a2}"a3=num1^num2 puts "XOR is #{a3}"a4= ~num2 puts "Complement is #{a4}"a5=num1>>num2 puts "left shift is #{a5}"a6=num1<<num2 puts "Right shift is #{a6}"

    Output

    輸出量

    Enter first value 3 Enter second value 5 and is 1 OR is 7 XOR is 6 Complement is -6 left shift is 0 Right shift is 96

    翻譯自: https://www.includehelp.com/ruby/operators.aspx

    ruby 三目運(yùn)算符

    總結(jié)

    以上是生活随笔為你收集整理的ruby 三目运算符_Ruby运算符的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。