Ruby Manifest [Draft]

Ruby Module and Class (Mix-in vs Inheritance)

That would be tricky to decide when to use module or inheritance in Ruby or any other language (behavior vs is-a relationship).

10/28/2022

Special note: It sounds more restrictive to design with class with inheritance. It is a linguistic challenge. A subclass has to be a type of superclass. Unlikely, the module provides more flexibility. It groups the behavior of a similar object. No discrimination.

What happens if the class and module have the same method? Which one the object will use?

The result from the above code:

foo from its class.
bar from module.

To sum up, the method lookup chain is in the following order:

How about defining the same method in the same class?

It will take the latest one. The bottom one.

foo 2

References:

array1 << value is different from array1 + [value]

For example:

arr1 = [1,2,3]
arr2 = arr1 << 4 # It is same as arr2 = arr1 = arr1 + [4]
puts arr1.inspect # => [1,2,3,4]
puts arr2.inspect # => [1,2,3,4]
arr3 = [1,2,3]
arr4 = arr3 + [4]
puts arr3.inspect # => [1,2,3]
puts arr4.inspect # => [1,2,3,4]

Ruby Bitwise and Logical Operation

Because Ruby is dynamic language, it is tricky with bitwise and logical operation unlike static languages such as Java. Here are the common notes about them in Ruby:

|| return first value if both value true

&& return second value if both value true

3 && 4 => 4false && 4 => false4 && false => false3 || 4 => 3false || 4 => 43 || false => 3# but be cautious between false and nil, it is reverse between || and &&false && nil => false 
nil && false => nil
false || nil => nil
nil || false => false
3 & 4 => 0 
false & 4 => false
3 | 4 => 7
false | 4 => true

Ruby Operations

Chic way to swap value between variables

a, b = b, c

--

--

Very passionate about writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store