Ruby language numbers, strings, and other

Source: Internet
Author: User
Tags numeric

In Ruby, everything is an object. More precisely, everything in Ruby is a fully functional object. So, in Ruby, the number 4, the fixed-point number 3.14, and the string "Hi" are all objects. Obviously, they are a bit "special" because you don't have to use the new method to create them. Instead, you use a form such as "literal 4" to create an instance of an object representing the number 4.

However, for the vast majority of people, it is useful to first understand the "standard" type provided by the language when learning a new programming language. So, in this section, we'll start with numeric types, string types, Boolean types, and other basic ruby data types.

Number Type

In essence, the numbers in Ruby are divided into integers and floating-point number two categories. Where integers are further subdivided into "general size" integers and large integers. Because everything is an object in Ruby, integers and floating-point numbers are defined by class (see Figure 1). As you see in Figure 1, numeric is the base class for all numeric types, and float and integer classes are subclasses of numeric. Both Fixnum and bignum are subtypes of integers-they define the "General size" integer and the large integer respectively.

Figure 1. Ruby's numeric type class inheritance diagram

Literal is used to describe instances of these classes. The following code in the interactive Ruby Shell (IRB) shows the literal instance of Float,fixnum and Bignum. Note that a method call can be made on the literal (in this case, the class method).

IRB (main): 001:0>3.class
=>fixnum
IRB (main): 002:0>3.4.class
=>float
IRB (main): 003:0 >10000000000000000000.class
=>bignum

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.