SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Class extending

  1. #1
    SitePoint Enthusiast sinatosk's Avatar
    Join Date
    Jul 2003
    Location
    PHP World
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class extending

    just playing around with ruby ( experimenting ). hopefully someone can help anyways... everytime I do this code

    class MYLIB
    def s
    self.to_s
    end

    def i
    self.to_i
    end

    def f
    self.to_f
    end
    end

    class String < MYLIB

    end

    class Integer < MYLIB

    end
    I keep getting this error

    classes.rb:15: superclass mismatch for class String (TypeError)
    line 15 being this code

    class String < MYLIB

  2. #2
    SitePoint Zealot bronze trophy
    Join Date
    Jun 2004
    Location
    Stockholm, Sweden
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    String (and Integer) already has a superclass, and I don't think you can change that dynamically at runtime.

    Why would you do this anyway? It seems like poor application design, or that you are trying to use a feature (class extensions) just for the sake of using it.
    If there is a way to overcome the suffering, there is no need to worry; if there is no way to overcome the suffering, there is no point to worry.
    - Shantideva

  3. #3
    SitePoint Enthusiast sinatosk's Avatar
    Join Date
    Jul 2003
    Location
    PHP World
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    like I said... I'm just playing around... experimenting

  4. #4
    SitePoint Zealot bronze trophy
    Join Date
    Jun 2004
    Location
    Stockholm, Sweden
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to experiment with class extensions you could try something like this:

    PHP Code:
    class Array
        
    def puts_each
            self
    .each do |item|
                
    puts item.to_s
            end
        end    
    end

    ["one","two","three"].puts_each 
    Remember that you are only extending an existing class, you cannot change it (make it a subclass of something else, for example).
    If there is a way to overcome the suffering, there is no need to worry; if there is no way to overcome the suffering, there is no point to worry.
    - Shantideva

  5. #5
    SitePoint Enthusiast sinatosk's Avatar
    Join Date
    Jul 2003
    Location
    PHP World
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx's I've been doing that kinda of stuff already

    just that when I got this code

    def s
    self.to_s
    end

    def i
    self.to_i
    end

    def f
    self.to_f
    end
    inside 3 seperate classes ( String, Integer, Float ) ... kinda makes the code abit bloated... so I thought I just do a class which has them in and add them on to the other classes considering all 3 classes gonna be doing the same when calling to them

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a better way of doing that - modules and mixins.

    Code:
    module LazyConversions
      def s
        self.to_s
      end
    
      def i
        self.to_i
      end
    
      def f
        self.to_f
      end
    end
    
    class String
      include LazyConversions
    end
    
    class Integer
      include LazyConversions
    end
    
    1.s    #=>   "1"

  7. #7
    SitePoint Enthusiast sinatosk's Avatar
    Join Date
    Jul 2003
    Location
    PHP World
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah thx's... didn't know about the "include" part... thats what I basically needed


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
  •