Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:52:11+00:00 2026-05-27T15:52:11+00:00

Bruce gives the following example in Seven languages in seven weeks, Ruby day 3,

  • 0

Bruce gives the following example in “Seven languages in seven weeks”, Ruby day 3, page 38:

module ActsAsCsv
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def acts_as_csv
      include InstanceMethods
    end
  end

  module InstanceMethods
    def read
       #fill @csv_contents and @headers from file self.class.to_s.downcase + '.txt'
    end

    attr_accessor :headers, :csv_contents

    def initialize
      read
    end
  end
end

class RubyCsv
  include ActsAsCsv
  acts_as_csv
end

m = RubyCsv.new
puts m.headers.inspect
puts m.csv_contents.inspect

I do not see any reason for the indirection used by def self.included(base) and ClassMethods. Is there an advantage of the above code over simply including the module InstanceMethods?


In detail: With “simply including the module InstanceMethods” I mean the following code:

  module InstanceMethods #defined as above
    def read
       #fill @csv_contents and @headers from file self.class.to_s.downcase + '.txt'
    end

    attr_accessor :headers, :csv_contents

    def initialize
      read
    end    
  end

class RubyCsv
  include InstanceMethods
end

m = RubyCsv.new
puts m.headers.inspect
puts m.csv_contents.inspect

Because of duck typing, isn’t this just as good as going via base.extend ClassMethods?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:52:12+00:00Added an answer on May 27, 2026 at 3:52 pm

    The original code allows you to do this:

    class A
      include ActsAsCsv
    end
    
    class B < A
      acts_as_csv
    end
    

    While using inheritance is more a matter of taste in this case it would be cleaner if

    • more methods than just acts_as_csv would be added similarly to A
    • ActsAsCsv would be expected to be included by a class from which many classes inherit (like some kind of BaseModel)

    Further advantage arise when

    • the method acts_as_csv is more elaborate than just including InstanceMethods
    • acts_as_csv takes an argument, for example acts_as_csv :separator => " "
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read a chapter in a book (Seven languages in Seven Weeks by Bruce
I'm working my way through Bruce Tate's Seven Languages in Seven Weeks and am
I am doing a brute force search for gradient extremals on the following example
Taking a dictionary as an example, let's say I've got the following array to
I've been reading Bruce Eckel's Thinking In Java and in the chapter on generics,
I'm reading Thinking in C++ by Bruce Eckel. In Chapter 15 (Volume 1) under
Learning a bit about Linq. I have the following code: (Please excuse the pathetic
I essentially have a problem which boils down to the following: Given some (integer)
Say I have the following table, used to get a list of delta updates
In 'Thinking in C++' by Bruce Eckel, there is a program given to print

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.