diff options
author | Taketo Takashima <[email protected]> | 2023-11-16 17:53:32 +0900 |
---|---|---|
committer | git <[email protected]> | 2024-10-18 15:00:37 +0000 |
commit | eb8cf1d60e4436bfebf52fbf4342ee6e5e18ba05 (patch) | |
tree | 8ad188409375a4ff69c7acf54bc960287d1d6c44 /lib/ipaddr.rb | |
parent | 6b1268c7bf0816a250991593f1e504db428b6e30 (diff) |
[ruby/ipaddr] Added to_json/as_json method
Updated to use cidr method when return address with prefix in #as_json
https://github.com/ruby/ipaddr/commit/cf8181d53e
Diffstat (limited to 'lib/ipaddr.rb')
-rw-r--r-- | lib/ipaddr.rb | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index dbb213c90a..b40d5c0359 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -227,6 +227,22 @@ class IPAddr return str end + # Returns a string containing the IP address representation with prefix. + def as_json(*) + if ipv4? && prefix == 32 + to_s + elsif ipv6? && prefix == 128 + to_s + else + cidr + end + end + + # Returns a json string containing the IP address representation. + def to_json(*) + format("\"%s\"", as_json) + end + # Returns a string containing the IP address representation in # cidr notation def cidr |