Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the common concept of Ruby core class

Shulou Source: shulou.com Published: 2022-06-02 03:13:59 09月16日 Update

What is the common core class concept of Ruby? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Classes and objects in Ruby language is a very important knowledge point. To learn the Ruby language, the first thing to master is the use of Ruby objects and classes. Here we will learn about the introduction of Ruby core classes.

Ruby core class I, Array

1. Instantiation

1) Array.new

2) [value 1, value 2, value 3, value 4, value 5]

3)% w [red green blue balck] automatically converts the list of strings into an array, omitting quotation marks and commas.

two。 Method

1) empty? Return true or false

2) size return size

3) first returns the first

4) last returns the last one

5) delete "element" deletion

3. Tip: view class help enter commands ri class name example ri Array

Ruby core class II. Hash: save key-value pairs

1. Instantiation

1) Hash.new

2) {key = > value, key = > value}

two。 Method

1) empty?

2) size

3) keys: return all key

4) values returns all value

Ruby core class 3. String: use single or double quotation marks

1. To include special symbols such as quotation marks in a string, you need to escape. "

two。 Shortcut:% Q (string) example:% Q (Arnie said, "Ihumm back!")

3. The Ruby expression # {expression} can be embedded in a string, for example: "current time: # {Time.now}"

Note that there can only be double quotation marks, and single quotation marks will output the expression as is

4. Method

Gsub (value 1, value 2), replacing the value 1 in the string with the value 2

Include? (value) whether it contains

Slice (0p3) intercept

Ruby core class IV. Numeric: Float Fixnum Bignum

1. 123.integer? Whether it is an integer

2. 12.3.round is rounded

3. 8.zero? Whether it is 0

4. Convert 12.to_f to Float

5. Convert 11.3.to_i to Fixnum

Ruby core class 5. Symbol symbol

1. Equivalent to a string, but there is no way

two。 Mutual conversion

"fox" .to _ sym

: fox.to_s

After reading the above, have you mastered the methods of what the common Ruby core class concepts are? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Core character string quotation mark method expression common concept content instance object more symbol language problem special important helpless to this end rounded Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Docker Huawei vpn