3

I tried to get this result:

Car.class.name # => "Car"
Truck.class.name # => "Truck"
Van.class.name # => "Van" 
Car/Truck/Van.superclass.name # => "Vehicle" 

I did this:

require "rspec/autorun"

class Vehicle
end

class Car < Vehicle
end

class Van < Vehicle
end

class Truck < Vehicle
end

describe Car do
  describe ".class.name" do
    it "returns name of the class" do
      expect(Car.class.name).to eq "Car"
    end
  end
end

What am I missing about Ruby's class system in order to implement this?

1
  • Have you a link to the coding challenge? Commented Jan 16, 2018 at 17:33

1 Answer 1

4

Your intuition is good. At face value, the challenge seems incorrect, and if it's intended to be "a relatively simple code challenge," then I think that must be the case.

If the challenge is meant to be tricky, on the other hand, the specified result is certainly something that's possible in Ruby:

class Vehicle
  def self.class
    self
  end
end

class Car < Vehicle; end
class Van < Vehicle; end
class Truck < Vehicle; end

p Car.class.name # => "Car"
p Truck.class.name # => "Truck"
p Van.class.name # => "Van"
p Car.superclass.name # => "Vehicle"

Try it on repl.it: https://repl.it/@jrunning/VisibleMustyHapuka

However, knowing nothing more about the challenge or its source, it's impossible to say whether or not this is the intended solution.

Sign up to request clarification or add additional context in comments.

3 Comments

Good observation! Do you know if changing self.class messes anything up with regard to the way the class behaves on other contexts?
Yea I knew the Solution was simple, I over complicated it, and I had trouble getting around the "why" factor.
@SilvioMayolo I would definitely expect this to cause some unusual behavior with code that makes assumptions about the behavior of .class, which is a lot of code in the Ruby ecosystem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.