Skip to content

Instantly share code, notes, and snippets.

@jeremyolliver
Created February 15, 2013 20:56
Show Gist options
  • Save jeremyolliver/4963475 to your computer and use it in GitHub Desktop.
Save jeremyolliver/4963475 to your computer and use it in GitHub Desktop.
def self.coerce_rlimit_resource(resource)
if resource.is_a?(Symbol) && (const_name = "RLIMIT_#{resource}") && const_defined?(const_name)
resource = const_get(const_name)
else
raise ArgumentError, "invalid resource name #{resource}"
end
Rubinius::Type.coerce_to resource, Integer, :to_int
end
@jimsynz
Copy link

jimsynz commented Feb 15, 2013

This doesn't work, because arguments are allowed to not be Symbols, in which case they should be passed on through to the next step unaltered (ie don't raise).

Maybe something like:

def self.coerce_rlimit_resource(resource)
  if resource.is_a?(Symbol)
   constant_name = "RLIMIT_#{resource}"
   raise ArgumentError, "invalid resource name #{resource}" unless const_defined? constant_name
   resource = const_get constant_name
  end
  Rubinius::Type.coerce_to resource, Integer, :to_int
end

@jeremyolliver
Copy link
Author

Ah, I see. Yeah, I guess that's a little more concise than initial implementation

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