0

I'm quite new to ruby. I'm used to Java and C++ though. What I was trying to understand is how to use polymorphism of the language. And also rules of inheritance. I've made a simple working code but I can't understand how good is it. Are there any other ways? There are always other ways in ruby as far as I know.

What it does: compares children like children if they belong to the same class. Otherwise compares them like their parent.

Sorry if I've wasted your time. Googling didn't help, I can't make a good query.

class Hero
public 
#redefine comparison like this just for the sake of having it use protected method
def <=> hero 
    @mass <=> hero.get_mass
end

def initialize mass
    @mass = mass
end

protected
#this protected method is used for inheritance testing purposes
def get_mass
    @mass
end

end

class Finn < Hero

def initialize mass, sword_length
    @mass = mass
    @sword_length = sword_length
end
    # THIS! Am I doing this right?
def <=> finn
    if finn.class == self.class
        (@mass+@sword_length) <=> (finn.get_mass+finn.get_sword_length)
    else
        super(finn)
    end 
end
    # probably not...
protected

def get_sword_length
    @sword_length
end
end

class Jake < Hero

def initialize mass, fist_length
   @mass = mass
   @fist_length = fist_length
end

def <=> jake
    if jake.class == self.class
        (@mass+@fist_length) <=> (jake.get_mass+jake.get_fist_length)
    else
        super(jake)
    end
end

protected

def get_fist_length
    @fist_length
end

end

hero1 = Finn.new(10, 80)
hero4 = Jake.new(50, 18)
puts " error?  "+(hero1<=>hero4).to_s
Raj
  • 22,346
  • 14
  • 99
  • 142
Jamato
  • 170
  • 1
  • 2
  • 6

2 Answers2

0

I have tried to refactor your code, Here is how it looks:-

class Hero
  attr_reader :mass
  #  more info in attr_reader and attr_accessor is here http://stackoverflow.com/questions/5046831/why-use-rubys-attr-accessor-attr-reader-and-attr-writer

  def initialize mass
    @mass = mass
  end

  def <=> hero
    mass <=> hero.mass
  end
end

class Finn < Hero
  attr_reader :sword_length

  def initialize mass, sword_length
    @sword_length = sword_length
    super(mass)
  end

  def <=> finn
    if finn.class == self.class
      (@mass+@sword_length) <=> (finn.mass+finn.sword_length)
    else
      super(finn)
    end
  end
end

class Jake < Hero
  attr_reader :fist_length

  def initialize mass, fist_length
    @fist_length = fist_length
    super(mass)
  end

  def <=> jake
    #alternate dense way to do it
    jake.class == self.class ? ((@mass+@fist_length) <=> (jake.mass+jake.fist_length)) : super(jake)
  end

end

hero1 = Finn.new(10, 80)
hero4 = Jake.new(50, 18)
puts " error?  "+(hero1<=>hero4).to_s
Saurabh
  • 71,488
  • 40
  • 181
  • 244
0

A couple of notes:

Typically you would use an accessor of the same name as your instance variable rather than your get style naming.

Typically you would not check the class, but rather what the object can do.

class Hero
  def initialize(mass)
    @mass = mass
  end

  def <=>(hero)
    mass <=> hero.mass
  end

  protected #I put this as protected since you did, but I would likely leave it public

  attr_reader :mass
end


class Finn < Hero
  def initialize(mass, sword_length)
    @mass = mass
    @sword_length = sword_length
  end

  def <=>(finn)
    if finn.respond_to? :sword_length
      (mass + sword_length) <=> (finn.mass + finn.sword_length)
    else
      super(finn)
    end
  end

  protected

  attr_reader :sword_length
end


class Jake < Hero

  def initialize(mass, fist_length)
    @mass = mass
    @fist_length = fist_length
  end
  #edited 
  def <=>(jake)
    if jake.respond_to? :fist_length
      (mass + fist_length) <=> (jake.mass + jake.fist_length)
    else
      super(jake)
    end
  end

  protected

  attr_reader :fist_length
end
Jamato
  • 170
  • 1
  • 2
  • 6
Alex Peachey
  • 4,606
  • 22
  • 18
  • Thank you, exactly what I was asking for. Now I know where to dig all the info! =) – Jamato Apr 13 '14 at 17:31
  • I edited :fist_length and :sword_length to be symbols because that's how it works. Could you...err..approve this edit? – Jamato Apr 13 '14 at 18:17
  • Thanks Jamato. Sorry about that. I was a little sleepy when I answered this morning and that combined with the copy/pasting and reformatting from your original code let that slip through. Thanks for the edit. – Alex Peachey Apr 13 '14 at 20:40