11/08/2018, 21:15

Road to Ruby Silver (Part 5)

This is just some note for who want to get Ruby Silver certification. :) Part 1 Part 2 Part 3 Part 4 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 ...

This is just some note for who want to get Ruby Silver certification. :)

Part 1

Part 2

Part 3

Part 4

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 and Symbol
"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 of a and b
  • eql?: q.eql?(b) → compare value of a and value of b
"foo".equal?("foo") → false
"foo".eql?("foo") → true
:foo.equal?(:foo) → true
:foo.eql?(:foo) → true
  • == and ===
    • == is same with eql?
    • ===: 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
0