Quiz Stuff

Quiz 10, April 2, 2018; 3 4 minutes; 3 points total

  1. Write a Ruby class named Pair, as follows: Examples:
    >> p = Pair.new("abc", [1,2,3])
    
    >> p.eqsize     => true
    
    >> p.to_s       => "3 / 3"
    
    >> Pair.new([], {1=>2}).to_s    => "0 / 1"
    

EC ½ point: Tell me something about writing classes in Ruby not evidenced above.

EC ½ point: Provide Pair with "getters" a and b, to fetch the two values held.

Answers

  1. Write a Ruby class Pair.
    class Pair
        def initialize(a,b)
            @a,@b = a,b
        end
    
        def eqsize
            @a.size == @b.size
        end
    
        def to_s
            "#{@a.size} / #{@b.size}"
        end
    
        attr_reader :a, :b
    end
    
    EC ½ point: Tell me something about writing classes in Ruby not evidenced above.
    Class variables are denoted with a double sigil: @@
    EC ½ point: Provide Pair with "getters" a and b, to fetch the two values held.
    Included above, via attr_reader.