Instance to parse numeric types in Ruby and constants _ruby topics

Source: Internet
Author: User
Tags constant numeric square root

Numeric type (number)
integral Type (integer)
The integer type is divided into two, if within 31 digits (four bytes), that is the Fixnum instance. If more than, that is the Bignum instance.
Integers range from-230 to 230-1 or-262 to 262-1. Integers in this range are class Fixnum objects, and integers outside this range are stored in objects of class Bignum.
You can use an optional preamble before an integer, an optional basic metric (0 corresponds to the octal,0x corresponding to the hex,0b binary) followed by a string of digits. The underscore character is ignored in the numeric string.
You can get an ASCII character or an integer value for an escape sequence marked with a question mark.
Instance

123         # fixnum decimal
1_234        # Fixnum with underlined decimal
-500         # negative Fixnum 0377         # octal
0xFF         # hexadecimal
0b1011        # binary
"a". Ord # "       a" character encoding
? \ n         # line feed (0x0a) code
12345678901234567890 # Bignum
#整型 integer Below are some integer literal 
#字面量 (literal): The values that can be seen in the code, values, bool values, strings, etc. are called literal quantities 
#如以下的0, 1_000_000,0xa, etc. 
a1=0 

#带千分符的整型 
a2=1_000_000 

#其它进制的表示 
a3=0xa 
puts A1,a2 
puts A3 




=end


floating-point type
Ruby supports floating-point numbers. They are numbers with decimals. A floating-point number is an object of class float and can be any of the following.
Instance

123.4        # Floating-point value
1.0e6        # scientific notation
4E20         # not required
4e+20        # symbol before the index


#浮点型 
f1=0.0 
f2=2.1 
f3=1000000.1 


Arithmetic operations

Subtraction Operator: +-*/index operator * *
Indices do not have to be integers, for example

#指数算术 
puts 2** (1/4) #1与4的商为0, and then 2 of the 0 to 1 
puts 16** (1/4.0) #1与4.0 of the quotient 0.25 (One-fourth), and then open four square root

Ruby Constants
Constants begin with uppercase letters. Constants defined within a class or module can be accessed from within a class or module, and constants defined outside a class or module can be accessed globally.
Constants cannot be defined within a method. Referencing an uninitialized constant results in an error. Assigning a value to a constant that has already been initialized produces a warning.

#!/usr/bin/ruby
#-*-coding:utf-8-*-

class Example
  VAR1 =
  VAR2 = def show
    puts "the first constant Value is #{var1} "
    puts" the value of the second constant is #{var2} "end end
#"

Create object
object=example.new ()
object.show

Here, VAR1 and VAR2 are constants. This will produce the following results:

The value of the first constant is
200, and the second constant has a value of

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.