SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mixed up by Mixin

    I want to be able to do calculations like
    Code:
    puts 10.toKilos; puts 10.2.toKilos
    I tried this... It does not work
    Code:
    module Weights
       def toKilos
         self*2.2
       end       
       
       def toLbs
         self / 2.2
       end
       
    end
    
    class Fixnums
       include Weights
    end
    
    class Float
       include Weights
    end
    
    puts 10.toKilos
    I can put a method definition in both class Fixnum and class Float to get this functionality. How do I use mixins to get the same functionality

  2. #2
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um.. typo

    Fixnums to Fixnum changes the problem... aaaaaargh..

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Totally off topic, but the convention in Ruby land is to write methods as lowercase_underscore. So that should be to_kilos and to_lbs

  4. #4
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. will remember that


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
  •