Xóa bài viết
Bạn có chắc chắn muốn xóa bài viết này không ?
Xóa bình luận
Bạn có chắc chắn muốn xóa bình luận này không ?
Road to Ruby Silver (Part 5)
This is just some note for who want to get Ruby Silver certification. :)
Chapter 5: Built-in classes (組み込みクラス)
5.1. Object
class
5.1.1. Object's id
a = "foo"
a.object_id → 70257771679880
a.__id__ → 70257771679880
- Use object_id, we can know the different between
String
andSymbol
"foo".object_id → 70257768130540
"foo".object_id → 70257768151060
:foo.object_id → 1150428
:foo.object_id → 1150428
5.1.2. Class of object
"foo".class → String
:foo.class → Symbol
5.1.3. Compare objects
-
equal?
:a.equal?(b)
→ compare object_id ofa
andb
-
eql?
:q.eql?(b)
→ compare value ofa
and value ofb
"foo".equal?("foo") → false
"foo".eql?("foo") → true
:foo.equal?(:foo) → true
:foo.eql?(:foo) → true
-
==
and===
-
==
is same witheql?
-
===
: Normaly,===
can be used instead of==
. But the difference is:===
can be overwrite when you define a class.
-
2.3.0 :irb > class String
2.3.0 :irb?> def ==(str)
2.3.0 :irb?> false
2.3.0 :irb?> end
SyntaxError: (irb):38: syntax error, unexpected end-of-input, expecting keyword_end
2.3.0 :024 > class String
2.3.0 :025?> def ===(str)
2.3.0 :026?> return true
2.3.0 :027?> end
2.3.0 :028?> end
=> :===
2.3.0 :irb > "foo" === "foo1"
=> true
5.1.4. List methods of object
Ruby provides below methods:
- methods
- private_methods
- protected_methods
- public_methods
- singleton_methods
5.1.5. Clone or copy object
- clone
- dup
a = "str"
a.object_id → 70257775626380
b = a.clone
b.object_id → 70257775641080
a.dup.object_id → 70257780186600
The difference between clone
and dup
: https://stackoverflow.com/questions/10183370/whats-the-difference-between-rubys-dup-and-clone-methods
5.1.6. Instance variable
instance_variable_get
instance_variable_set
instance_variables
5.1.7. method_missing
class Bar
def method_missing(name, *args)
puts name
end
end
b = Bar.new
b.not_existed_method → not_existed_method
5.1.8. Change object to string
"1.2".to_s
Object.new.inspect
5.1.9. Special object
true.class → TrueClass
false.class → FalseClass
nil.class → NilClass
5.2. Numeric classes: Numeric, Integer, Fixnum, Bignum, Float
5.2.1. Numeric
class
- All instance methods of class
Numeric
2.3.0 :irb > Numeric.instance_methods(false)
=> [:%, :+@, :-@, :<=>, :eql?, :singleton_method_added, :to_int, :div, :coerce, :divmod, :i, :fdiv, :modulo, :remainder, :abs, :magnitude, :real?, :integer?, :zero?, :nonzero?, :floor, :ceil, :round, :truncate, :step, :positive?, :negative?, :quo, :numerator, :denominator, :arg, :rectangular, :rect, :polar, :real, :imaginary, :imag, :abs2, :angle, :phase, :conjugate, :conj, :to_c]
Below are some methods we should make attention to them.
- ceil
- floor
- round
- truncate
2.3.0 :001 > 1.9.ceil
=> 2
2.3.0 :002 > 1.9.floor
=> 1
2.3.0 :003 > 1.9.round
=> 2
2.3.0 :004 > 1.9.truncate
=> 1
2.3.0 :005 > -1.1.ceil
=> -1
2.3.0 :006 > -1.1.floor
=> -2
2.3.0 :007 > -1.1.truncate
=> -1
2.3.0 :008 > -1.1.round
=> -1
- abs
- step
Bình luận

{{ comment.user.name }}
Bỏ hay
Hay

Cùng một tác giả

17
2
Xin thân chào các thí chủ. Bần tăng lại trở lại và ăn hại gấp n lần. Dạo gần đây nhìn vào khuynh hướng tìm hiểu tech của thiên hạ, vẫn như mọi kh...

15
3
Sau 1 thời gian hơi sấp mặt 1 chút, bần tăng đã trở lại và ăn hại gấp n lần. Mở đầu Đối với các công ty cung cấp dịch vụ B2B, khách hàng là thượ...

11
6
Bần tăng, 1 dev Ruby đã có kinh nghiệm làm việc cho 1 công ty Nhật 2 năm. Trong 2 năm qua bần tăng đã học được rất nhiều điều mà bản thân ngày xưa ...
Bài viết liên quan

9
6
Chưa xem phần 2? Xem (Link) Trong bài viết này tôi giới thiệu cho các bạn về khái niệm function arity, một cách gọi mĩ miều của số lượng argument ...

9
1
Tiếp theo (Link) Mình sẽ hướng dẫn cách test căn bản cho API mình tạo. Thật ra mà nói thì mình phải viết test trước khi làm nhưng mà để tránh việc...

5
2
__Chú thích__: Đây là bản dịch tiếng Việt của bài viết gốc của tôi. Nếu bạn muốn xem bản tiếng Anh, xin hãy trỏ tới URL (Link) Lời mở (Link) là ...