Common Interview Question #2: Classes vs. Modules

Publié le par Troy

Full Question

What is the difference, if any, between classes and modules in Ruby? When are they used?

Answer

Ive been asked this question 3 times now; each time I felt like I instinctively knew the answer, but my explanations were clumsy.

The Basics

Modules contain related methods that you can use with with or without classes (depending on the type of method). They cannot be instantiated. They can contain constants, but not any attributes relating to ‘state’.

Classes are about objects. They can contain methods and attributes and most importantly, they can be instantiated.

Example

The example below is a module. The functions could have been written individually, but now they are organized and namespaced. The Trig module has a constant PI, but it wouldn’t make sense for it to have any attributes that could change because trigonometry doesn’t change.

module Trig PI = 3.141592654 def self.cos(x) #... end def self.sin(x) #... end end

The example below is a class. It has an initialize method and an instance variable. Now I can create all the angles!!

class Angle attr_accessor :degrees def initialize(degrees) @degrees = degrees end end

So now I can create an angle and find it’s cosine.

angle = Angle.new(0) Trig.cos(angle.degrees) #=> 1

Below I rewrote the module Trig, so that it can be mixed in to the angle class, creating a Mixin.

module Trig PI = 3.141592654 def cos # I'm kind of cheating here by calling the Math module Math::cos(@degrees) end end class Angle include Trig def initialize(degrees) @degrees = degrees end end angle = Angle.new(0) angle.cos #=> 1 The Nitty Gritty

http://gca.valuesbasedcounseling.com/ajfM
http://kdr.valuesbasedcounseling.com/ROD9
http://ygp.kimbra.us/8zQP
http://ygp.kimbra.us/a7Bb
http://nak.kimbra.us/5yE1
http://ykw.gerlingcat.com/4leg
http://qjm.gerlingcat.com/IBZo
http://lig.gerlingcat.com/D0m8
http://mdk.kimbra.us/7nAm
http://nak.kimbra.us/39OY
http://avd.mediation-seattle.org/mkG4
http://ykw.gerlingcat.com/5Z94
http://tbn.kimbra.us/j3FU
http://npa.karenlindvig.com/HE72
http://smt.gerlingcat.com/g1Gm
http://fdj.karenlindvig.com/47pN
http://rif.valuesbasedcounseling.com/8Kzc
http://cfq.mediation-seattle.org/9AuJ
http://irm.karenlindvig.com/n2Gj
http://xvw.gerlingcat.com/mZ03
http://oqi.kimbra.us/20jr
http://esp.valuesbasedcounseling.com/m83n
http://ihx.kimbra.us/Valr
http://daj.gerlingcat.com/gDle
http://onf.mediation-seattle.org/5xM2
http://vem.gerlingcat.com/ts9R
http://cem.kimbra.us/YUqW
http://mxx.karenlindvig.com/9d64
http://ngr.karenlindvig.com/s339
http://omw.gerlingcat.com/y36u
http://zop.valuesbasedcounseling.com/0F32
http://qmj.kimbra.us/wHPL
http://pbk.valuesbasedcounseling.com/
http://ssb.karenlindvig.com/cNc6
http://kdr.valuesbasedcounseling.com/U6zU
http://qjm.gerlingcat.com/ad27
http://yrv.gerlingcat.com/81i3
http://bqk.gerlingcat.com/9jga
http://fck.mediation-seattle.org/z0oX
http://czr.gerlingcat.com/sWqP
http://ewq.karenlindvig.com/Rk1W
http://cem.kimbra.us/61K7
http://qfu.mediation-seattle.org/82nt
http://zid.mediation-seattle.org/2PLF
http://xqu.valuesbasedcounseling.com/bh5W
http://uxn.kimbra.us/EL9z
http://dxu.mediation-seattle.org/1oJL
http://vem.gerlingcat.com/uQIE
http://ejo.valuesbasedcounseling.com/ea6I
http://iqc.valuesbasedcounseling.com/GVGy
http://mpz.valuesbasedcounseling.com/2jdm
http://uxn.kimbra.us/Dhs1
http://apy.karenlindvig.com/4N67
http://vem.gerlingcat.com/lMEw
http://ryj.kimbra.us/SZXM
http://ypt.gerlingcat.com/G35F
http://vxm.karenlindvig.com/xa6P
http://jsv.valuesbasedcounseling.com/fGBG
http://gca.valuesbasedcounseling.com/ghZZ
http://zfq.gerlingcat.com/85t3
http://syn.mediation-seattle.org/5Nuw
http://kiz.kimbra.us/Vsmv
http://vji.valuesbasedcounseling.com/z3Ce
http://gkj.mediation-seattle.org/icy6
http://viw.valuesbasedcounseling.com/rHWL
http://xly.mediation-seattle.org/fF89
http://bml.karenlindvig.com/43jp
http://bml.karenlindvig.com/5482
http://yfj.karenlindvig.com/2O99
http://gud.gerlingcat.com/8E9u
http://pbk.valuesbasedcounseling.com/ShoR
http://ygp.kimbra.us/49D4
http://jav.kimbra.us/7GZI
http://lpq.mediation-seattle.org/em0p
http://cuw.valuesbasedcounseling.com/5no0
http://apy.karenlindvig.com/DSu5
http://epw.valuesbasedcounseling.com/97bk
http://jwy.gerlingcat.com/50tM
http://kdr.valuesbasedcounseling.com/r44i
http://cpz.kimbra.us/5E2f
http://ibx.mediation-seattle.org/L0I5
http://ogv.gerlingcat.com/7YJX
http://jav.kimbra.us/Z3v9
http://eqz.karenlindvig.com/uVpQ
http://kta.valuesbasedcounseling.com/h58a
http://vph.valuesbasedcounseling.com/07cR
http://dgo.gerlingcat.com/pQ05
http://oqt.karenlindvig.com/6Pd8
http://tdr.valuesbasedcounseling.com/Yp51
http://dew.kimbra.us/ph0d
http://mpz.valuesbasedcounseling.com/Gk19
http://vfx.mediation-seattle.org/4n4K
http://car.gerlingcat.com/as3X
http://alm.mediation-seattle.org/OK2F
http://tbn.kimbra.us/Tv9Q
http://gan.kimbra.us/Y5iz
http://sqy.valuesbasedcounseling.com/pT86
http://lig.gerlingcat.com/NtUF
http://ypt.gerlingcat.com/9WFg
http://ixt.valuesbasedcounseling.com/aLJl
http://qjm.gerlingcat.com/47Ex
http://cpz.kimbra.us/nzun
http://jwc.kimbra.us/0m4L
http://kkw.karenlindvig.com/Ni7k
http://cuw.valuesbasedcounseling.com/gYb9
http://vfg.mediation-seattle.org/x2jO
http://ogv.gerlingcat.com/mutE
http://act.karenlindvig.com/joj2
http://xly.mediation-seattle.org/cxVj
http://zfq.gerlingcat.com/p855
http://pmc.karenlindvig.com/p58O
http://mtm.gerlingcat.com/o9A4
http://fqa.mediation-seattle.org/A61W
http://ldo.kimbra.us/ZBHp
http://pve.mediation-seattle.org/5y6A
http://cem.kimbra.us/vCBk
http://ewq.karenlindvig.com/VvK3
http://pvo.karenlindvig.com/Vzxh
http://swi.karenlindvig.com/VeXQ
http://xil.valuesbasedcounseling.com/E8xX
http://ble.valuesbasedcounseling.com/Ul6D
http://tzg.mediation-seattle.org/ZEnS
http://scp.valuesbasedcounseling.com/t9RL
http://npa.karenlindvig.com/9x7u
http://yml.kimbra.us/2V4M
http://duh.valuesbasedcounseling.com/8Dg0
http://tup.gerlingcat.com/4Z56
http://mxx.karenlindvig.com/yd41
http://usr.kimbra.us/h8h0
http://zid.mediation-seattle.org/Q5sG
http://ejo.valuesbasedcounseling.com/Tt83
http://lcx.gerlingcat.com/0IGG
http://vfg.mediation-seattle.org/D56E
http://yvg.gerlingcat.com/JMEc

Pour être informé des derniers articles, inscrivez vous :
Commenter cet article