https://github.com/interop-tokyo-shownet/maca
A class to manipulate a MAC Address in ruby
https://github.com/interop-tokyo-shownet/maca
macaddress ruby
Last synced: 11 months ago
JSON representation
A class to manipulate a MAC Address in ruby
- Host: GitHub
- URL: https://github.com/interop-tokyo-shownet/maca
- Owner: interop-tokyo-shownet
- License: mit
- Created: 2025-06-23T14:58:06.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-07-12T05:33:23.000Z (12 months ago)
- Last Synced: 2025-07-28T02:48:19.863Z (11 months ago)
- Topics: macaddress, ruby
- Language: Ruby
- Homepage:
- Size: 41 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Maca
maca provides a set of methods to manipulate a MAC Address.
## Installation
Install the gem and add to the application's Gemfile by executing:
```bash
bundle add maca
```
If bundler is not being used to manage dependencies, install the gem by executing:
```bash
gem install maca
```
## Usage
### Basic
Create a new `Macaddress` object from a MAC Address in various formats.
```ruby
require 'maca'
Macaddress.new("01:23:45:67:89:ab")
=> #
Macaddress.new("01:23:45:67:89:ab").to_s
=> "01:23:45:67:89:AB"
Macaddress.new("01:23:45:67:89:ab").to_i
=> 1250999896491
```
### Format String
**Format Normalization**
Normalize various MAC Address notations (hyphenated, dot-separated, raw hex, etc.) into the standard colon-separated format.
```ruby
Macaddress.new("00-00-00-00-00-00").to_s
=> "00:00:00:00:00:00"
Macaddress.new("000000000000").to_s
=> "00:00:00:00:00:00"
Macaddress.new("0000.0000.0000").to_s
=> "00:00:00:00:00:00"
Macaddress.new("000000-000000").to_s
=> "00:00:00:00:00:00"
```
**Format Conversion**
Convert the MAC Address to a custom string format using a specified delimiter and step size.
```ruby
Macaddress.new("00-00-00-00-00-00").to_fs(delimiter: '.', step: 4)
=> "0000.0000.0000"
```
### Comparison
Compare `Macaddress` objects for equality based on their normalized form.
```ruby
Macaddress.new("00:00:00:00:00:00") == Macaddress.new("00:00:00:00:00:00")
=> true
Macaddress.new("00:00:00:00:00:00") == Macaddress.new("00:00:00:00:00:01")
=> false
```
### Address type check
Determine the type of a MAC address, such as whether it is unicast or multicast, and whether it is locally or universally administered.
```ruby
Macaddress.new("00:00:00:00:00:00").unicast?
=> true
Macaddress.new("00:00:00:00:00:00").broadcast?
=> false
Macaddress.new("33:33:00:00:00:01").multicast?
=> true
Macaddress.new("02:00:00:00:00:00").locally_administered?
=> true
Macaddress.new("01:00:00:00:00:00").universally_administered?
=> true
Macaddress.new("02:00:00:00:00:00").random?
=> true
```
### OUI
Extract the Organizationally Unique Identifier (OUI) from a MAC address.
The `#oui` method returns the first 24 bits (or the first 3 octets) of a MAC Address, which typically identifies the device manufacturer or vendor.
```ruby
Macaddress.new("01:23:45:67:89:ab").oui # default format: :base16
=> "012345"
Macaddress.new("01:23:45:67:89:ab").oui(format: :hex)
=> "01-23-45"
```
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/interop-tokyo-shownet/maca.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).