SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Obtaining hash methods from a custom Class

    Thanks in advance for taking the time to even respond to this. I'm in the process of learning programming, Ruby language to be specific.

    I am trying to understand the correct way to set up class definitions.

    The attached code is meant to open a .csv file, store the two fields in a Structure, and then convert the structure to a hash.

    I am trying to wrap the process into the def initiator of my class. When the new object is created, there are not any methods I can use on it. I keep getting method not defined errors when trying to use the Hash methods.

    How do I set this up so I can add Hash methods to my class CellLookup?

    Code Ruby:
    IMPORT_CELLS = "Omaha_Equip.txt"
     
    ##Strucutre to hold imported cell names
    Struct.new('Equipment', :number, :name)
     
     
     
    class CellLookup 
    attr_reader :cell_hash
    #
      def initialize(cell_list)
        cells = []
        File.open(cell_list,"r").each_line do |line|
    	  new = line.chomp.split(",")
    	  equipnum = new[0]
    	  equipname = new[1]
    	  cells << (Struct::Equipment.new(equipnum, equipname))
        end
     
    #convert file to hash with key=Cell BTS Num and Value = Struct::EquipName
        cell_hash = cells.inject({}) do |hash,iterate| 
    	  (hash[iterate.number] ||= []) << iterate
    	  hash  
        end
      @cell_hash = cell_hash
      end
    end
     
     
    import = CellLookup.new(IMPORT_CELLS)
     
    puts import.keys

    Thanks in advance for any advice. BTW, this is for an alarm log parser for a wireless telephone switch, although it's mainly a learning exercise.
    Last edited by jschro02; Apr 12, 2008 at 09:20.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're calling import.keys. This should be import.cell_hash.keys.

    Your code can be simplified considerably by using Ruby's CSV module:

    Code ruby:
    hash = Hash[*CSV.read('Omaha_Equip.txt').flatten]

    This reads the CSV file Omaha_Equip.txt into a hash. The hash's keys are the first elements of each row, the values are the second elements.

    If Omaha_Equip.txt looks like this:

    Code:
    a,1
    b,2
    c,3
    Then the hash will look like this:

    Code ruby:
    {"a"=>"1", "b"=>"2", "c"=>"3"}

    All in just one line of code

  3. #3
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, I was able to condense what I had done initially quite a bit. I didn't use the method recommended above because I would like to read my .csv file into a Data Strucuture for easy use elsewhere.

    my Omaha.txt looks like this:
    Code:
    MC800BTS1000,County Line,855000
    MC800BTS1001,Craig,855001
    MC800BTS1002,Greys Lake,855002
    The code I came up with for bringing it into a hash looks like this:
    Code Ruby:
    Struct.new('Equipment', :num, :name, :id) #structure to hold  BTS#, site name, and financeid
     
    cell_hash = {} #The hash I want to use
     
    CSV.open('Omaha_Equip.txt','r') do |line|  #populate the hash from Omaha_Equip.txt file
      num,name,id = line
      cell_hash[num] = Struct::Equipment.new(num, name, id)
      end

    However, I am still a bit perplexed as to how I would create a Class around this. I would like to be able to do something like this:

    Code Ruby:
    omaha_lookup = ClassLookup.new(TEXTFILE)

    my omaha_lookup variable would then contain the hash. However, I do not know how to do this and get the code so all the Hash methods will be available to objects of class ClassLookup (ie, so omaha_lookup.keys, for example) would actually work.

    Sorry if this is a very noobish question, but I'm a very noobish coder.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also use a function, like this:

    Code ruby:
    Equipment = Struct.new(:num, :name, :id)
     
    def parse_file(file) # use a more descriptive name instead of parse_file
      cell_hash = {}
      CSV.open(file, 'r') do |line|
        num, name, id=  line
        cell_hash[num] = Equipment.new(num, name, id)
      end
    end
     
    omaha_lookup = parse_file(TEXTFILE)

    This just defines a function that returns the hash itself. If you want to have a special class with all hash methods you can subclass Hash:

    Code ruby:
    class ClassLookup < Hash # or a better name?
      def initialize(file)
        CSV.open(file, 'r') do |line|
          num, name, id = line
          self[num] = Equipment.new(num, name, id)
        end
      end
    end
     
    omaha_lookup = ClassLookup.new(TEXTFILE)

  5. #5
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I was very confused as to how to do this.

    I was wanting to do this as a class, but I see that it doesn't necessarily make a lot of sense to do so in this particular situation.

    Since there is only one method to the ClassLookup class, would it make more sense to simply do this as a function, as you initially showed in your example?

    I'm still learning about all this, and deciding when to make a class and when to do just a function is still a little fuzzy.

    Thanks, this has been very helpful.

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use a function in this case, because you're not really creating a new data structure. It's often not clear what's the best (simplest) solution until you've implemented both...

  7. #7
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You certainly have been helpful. Thanks for the time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •