I am coming from the Java world but I tried to use a base class like this...
class Bicycle
attr_reader :gears
def initialize( gears: 1,
seats: 2,
wheels: 2 )
@gears = gears
@seats = seats
@wheels = wheels
end
end
class CrazyBicycle < Bicycle
def initialize( seats: 2,
wheels: 2 )
super(101, seats, wheels)
end
end
b = CrazyBicycle.new(3, 4)
puts b.gears
This didn't work and throws Wrong number of arguments (given: 2, expected: 0)
. I can get it to work by being extremely explicit with param names. For example this does work...
class Bicycle
attr_reader :gears
def initialize( gears: 1,
seats: 2,
wheels: 2 )
@gears = gears
@seats = seats
@wheels = wheels
end
end
class CrazyBicycle < Bicycle
def initialize( seats: 2,
wheels: 2 )
super(gears: 101, seats: seats, wheels:wheels)
end
end
b = CrazyBicycle.new(seats: 3,wheels: 4)
puts b.gears
But that seems like a lot of unnecessary variable names. Is there some sort of Ruby sugar I can use to prevent being that explicit?