Skip to content

Latest commit

 

History

History
67 lines (42 loc) · 818 Bytes

module-method-book-publish(easy).md

File metadata and controls

67 lines (42 loc) · 818 Bytes

Question

module Book  
  def price(n) 
    return n
  end  

  def author(a)
    return a
  end      
end  

class Library  
 include Book 

  def publish  
    puts(price(200))  
    puts(author("Google"))  
  end  
end  

a) In the above coding, if you need to use author method as both an instance method and as a module method, how will you do?

b) Show how to access the author method with an object and without an object?

Solution

module Book  
  def price(n) 
    return n
  end  

  def author(a)
    return a
  end  
  
 module_function :author  #Both instance and module method
end  

class Library  
 include Book  

  def publish  
    puts(price(200))  
    puts(author("Google"))  
  end  
end  

ob = Library.new  
ob.publish  
puts(Book.author("Amazon"))