Hacker News new | past | comments | ask | show | jobs | submit login

    class Animal
      def initialize(options = {})
        @type = options[:type]
        @name = options[:name]
        @legs = options[:legs]
        @noise = options[:noise]
      end
  
      def type?
        return @type
      end
  
      def name?
        return @name
      end
  
      def noise?
        return @noise
      end
  
      def legs?
        return @legs
      end
    end

    dog = Animal.new(:type => "Dog", :name => "Pipin", :legs => 4, :noise => "Woof")
    puts "#{dog.name?} the #{dog.type?} has #{dog.legs?} legs and goes \"#{dog.noise?}!\"\n"

    cow = Animal.new(:type => "Cow", :name => "Kevin", :legs => 4, :noise => "Moo")
    puts "#{cow.name?} the #{cow.type?} has #{cow.legs?} legs and goes \"#{cow.noise?}!\"\n"
Output:

    Pipin the Dog has 4 legs and goes "Woof!"
    Kevin the Cow has 4 legs and goes "Moo!"
…a simple class which turns the dog and cow variables into objects, like small boxes with information referring to that animal. By the way — I hope code isn't classed as words, else I've failed to describe it in under 64! :P



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: