Method: ELFTools::Util::ClassMethods#to_constant

Defined in:
lib/elftools/util.rb

#to_constant(mod, val) ⇒ Integer

Fetch the correct value from module mod.

See ELFFile#segment_by_type for how to use this method.

Parameters:

  • mod (Module)

    The module defined constant numbers.

  • val (Integer, Symbol, String)

    Desired value.

Returns:

  • (Integer)

    Currently this method always return a value from Constants.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/elftools/util.rb', line 36

def to_constant(mod, val)
  # Ignore the outest name.
  module_name = mod.name.sub('ELFTools::', '')
  # if val is an integer, check if exists in mod
  if val.is_a?(Integer)
    return val if mod.constants.any? { |c| mod.const_get(c) == val }

    raise ArgumentError, "No constants in #{module_name} is #{val}"
  end
  val = val.to_s.upcase
  prefix = module_name.split('::')[-1]
  val = "#{prefix}_#{val}" unless val.start_with?(prefix)
  val = val.to_sym
  raise ArgumentError, "No constants in #{module_name} named \"#{val}\"" unless mod.const_defined?(val)

  mod.const_get(val)
end