Pass value to parent in class declaration in Ruby

I watched a screencast with Jim Weirich, where he started to do something like this:

class Subuser < User("Type")
end

Does Ruby Allow Passing Arguments When Defining a Parent Class? I cannot come up with an example where this really works.

+5
source share
1 answer

This can be done by declaring a method Userthat takes an argument and returns a class:

class Admin
end

class Client
end

def User(arg)
  case arg
    when :admin
      Admin
    when :client
      Client
  end
end

class Subuser < User(:admin)
end

Subuser.superclass
# => Admin
+7
source

All Articles