Class: Y2Network::IPAddress
- Inherits:
-
Object
- Object
- Y2Network::IPAddress
- Extended by:
- Forwardable
- Defined in:
- src/lib/y2network/ip_address.rb
Overview
This class represents an IP address
The IPAddr from the Ruby standard library drops the host bits according to the netmask. The problem is that YaST uses a CIDR-like string, including the host bits, to set IPADDR in ifcfg-* files (see man 5 ifcfg for further details).
However, what we need is to be able to keep the host part
Instance Attribute Summary collapse
-
#address ⇒ IPAddr
IP address.
-
#prefix ⇒ Integer
Prefix.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Determines whether two addresses are equivalent.
-
#initialize(address, prefix = nil) ⇒ IPAddress
constructor
Constructor.
-
#netmask=(netmask) ⇒ Object
Sets the prefix from a netmask.
-
#prefix? ⇒ Boolean
Determines whether a prefix is defined.
-
#to_s ⇒ Object
Returns a string representation of the address.
Constructor Details
#initialize(address, prefix = nil) ⇒ IPAddress
Constructor
69 70 71 72 |
# File 'src/lib/y2network/ip_address.rb', line 69 def initialize(address, prefix = nil) @address = IPAddr.new(address) @prefix = prefix end |
Instance Attribute Details
#address ⇒ IPAddr
Returns IP address.
50 51 52 |
# File 'src/lib/y2network/ip_address.rb', line 50 def address @address end |
#prefix ⇒ Integer
Returns Prefix.
52 53 54 |
# File 'src/lib/y2network/ip_address.rb', line 52 def prefix @prefix end |
Class Method Details
.from_string(str) ⇒ Object
57 58 59 60 61 |
# File 'src/lib/y2network/ip_address.rb', line 57 def from_string(str) address, prefix = str.split("/") prefix = prefix.to_i if prefix new(address, prefix) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Determines whether two addresses are equivalent
97 98 99 |
# File 'src/lib/y2network/ip_address.rb', line 97 def ==(other) address == other.address && prefix == other.prefix end |
#netmask=(netmask) ⇒ Object
Sets the prefix from a netmask
82 83 84 |
# File 'src/lib/y2network/ip_address.rb', line 82 def netmask=(netmask) self.prefix = IPAddr.new("#{netmask}/#{netmask}").prefix end |
#prefix? ⇒ Boolean
Determines whether a prefix is defined
106 107 108 |
# File 'src/lib/y2network/ip_address.rb', line 106 def prefix? !!@prefix end |
#to_s ⇒ Object
Returns a string representation of the address
75 76 77 |
# File 'src/lib/y2network/ip_address.rb', line 75 def to_s prefix? ? "#{@address}/#{@prefix}" : @address.to_s end |