Learning Ruby

Reference, Mnemonic & Ramblings

Before Developing My First Object Oriented Application: Notes to Self

These are generic notes made when i learnt the basics of Object Oriented programming using Ruby.

  • Modules can reference public instance methods of the class, for e.g.
Modules Referencing Instance Methods
1
2
3
4
5
6
7
8
9
10
11
12
13
module Walkable
  def walk
    "#{name} is walking"
  end
end

Class Dog
  attr_accessor :name
  include Walkable
  def initialize(n)
    @name = n
  end
end

The above will work ONLY if the class that implements the module Walkable has a public method name (which is a getter in this case). It is a good practice to put a comment to that effect where the module is defined.

  • Only two things evaluate to the boolean false in Ruby, they are a null object nil and false

  • Every expression returns something in Ruby. The return value is shown after an hash-rocket sign => in an IRB session after executing a line of code.

  • It's a good idea to just return a string from a method and then using puts on the method call to display the desired string rather than doing a puts in the method itself.

  • A good way to start developing/design an OO application is following the steps below:
    1. Write the problem statement for the application
    2. Extract nouns from the problem statement: can be good indicators for required classes
    3. Extract verbs and associate them with the nouns: can be good indicators of required methods and responsibilities

Note: The above method was put to use here

comments powered by Disqus