Skip to content

Instantly share code, notes, and snippets.

@saturnflyer
Created July 10, 2013 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saturnflyer/5966613 to your computer and use it in GitHub Desktop.
Save saturnflyer/5966613 to your computer and use it in GitHub Desktop.
Dynamically defining the initialize method and setting the arity.
class Something
def self.init(*init_args)
# this works but...
define_method(:initialize){|*args|
# arity is -1
}
class_eval %Q<
def initialize(#{*init_args})
# this doesn't work
end
>
end
end
# Here I want arity of :initialize to be 2
class Special < Something
init(:one, :two)
end
# And here I want arity of :initialize to be 3
class Other < Something
init(:one, :two, :three)
end
@saturnflyer
Copy link
Author

thanks guys! @mulder's comment work. I wasn't joining the args

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment