Class: Pwnlib::Constants::Constant

Inherits:
Numeric
  • Object
show all
Defined in:
lib/pwnlib/constants/constant.rb

Overview

A class that includes name and value representing a constant. This class works like an integer, and support operations with integers.

Examples:

a = Pwnlib::Constants::Constant.new('a', 0x3)
#=> Constant("a", 0x3)
[a + 1, 2 * a, a | 6, a == 3, 0 > a]
#=> [4, 6, 7, true, false]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, val) ⇒ Constant

Returns a new instance of Constant.

Parameters:

  • str (String)
  • val (Integer)


25
26
27
28
29
# File 'lib/pwnlib/constants/constant.rb', line 25

def initialize(str, val)
  super()
  @str = str
  @val = val
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

We don’t need to fall back to super for this, so just disable the lint.



32
33
34
# File 'lib/pwnlib/constants/constant.rb', line 32

def method_missing(method, *args, &block)
  @val.__send__(method, *args, &block)
end

Instance Attribute Details

#strString (readonly)

Returns:

  • (String)


18
19
20
# File 'lib/pwnlib/constants/constant.rb', line 18

def str
  @str
end

#valInteger (readonly)

Returns:

  • (Integer)


21
22
23
# File 'lib/pwnlib/constants/constant.rb', line 21

def val
  @val
end

Instance Method Details

#<=>(other) ⇒ Object



56
57
58
# File 'lib/pwnlib/constants/constant.rb', line 56

def <=>(other)
  to_i <=> other.to_i
end

#coerce(other) ⇒ Object



40
41
42
# File 'lib/pwnlib/constants/constant.rb', line 40

def coerce(other)
  [other.to_i, to_i]
end

#inspectObject



52
53
54
# File 'lib/pwnlib/constants/constant.rb', line 52

def inspect
  format('Constant(%p, %s)', @str, ::Pwnlib::Util::Fiddling.hex(@val))
end

#respond_to_missing?(method, include_all) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/pwnlib/constants/constant.rb', line 36

def respond_to_missing?(method, include_all)
  @val.respond_to?(method, include_all)
end

#to_iObject



44
45
46
# File 'lib/pwnlib/constants/constant.rb', line 44

def to_i
  @val
end

#to_sObject



48
49
50
# File 'lib/pwnlib/constants/constant.rb', line 48

def to_s
  @str
end