Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

class inheriting multiple modules in ruby

First, two modules included:

module M; end
module N; end
class C
  include M
  include N
end

C.ancestors # => [C, N, M, Object, Kernel, BasicObject]
So the methods will first be searched for in C. If a method with the given name is not found, it is searched for first in N and then in M. In other words - the reverse order of which you included the modules.

Second, module, including a module, included in a class:

module X; end
module Y
  include X
end
class K
  include Y
end

K.ancestors # => [K, Y, X, Object, Kernel, BasicObject]
So we can see that the same rule applies for including in modules. Just as in the previous example a method will first be searched for in C and only then in the modules included in C, here a method will first will be searched for in a module, and only then in the included modules in that module.

The reason for that, other than consistency, is that classes are actually modules in Ruby:

Class.superclass # => Module
Comment

PREVIOUS NEXT
Code Example
Ruby :: set db environment to development 
Ruby :: ruby rails remove tmp/pids/server.pid 
Ruby :: how to overwrite last line of console in ubuntu rails 
Ruby :: ruby classes 
Ruby :: how to comment out embedded ruby 
Ruby :: apple calendar gem in rails 
Ruby :: logstash-logger gem 
Ruby :: ruby convert array to set 
Ruby :: ruby puts format 
Ruby :: ruby check if string is integer 
Ruby :: rails class sti reminders 
R :: r ggplot regression line 
R :: r remove leading and trailing whitespace 
R :: how to re-arrange weekdays in r 
R :: extract r squared from lm in r 
R :: r rename columns 
R :: how to split a column in r 
R :: how to create dictionary in R 
R :: remove row from matrix r 
R :: r environment variables 
R :: subset row r 
R :: read file in r EOF within quoted string 
R :: stat_poly_eq position 
R :: calculated defualt values in R function parameters 
R :: R create sequence of date each quarters 
R :: return the name of the dataset in r 
R :: extract residual standard error from lm in r 
R :: vector with real numbers R 
R :: R caTools library with Mandelbrot set 
Rust :: how to cahce clean cargo 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =