Enhancement: add constants for IPv4 and IPv6 subnet masks

未关闭
#83 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@datibbaw 已经在做这个了。

开始于 2026年11月19日。

  • #117 来自 @PharaohKJ —— 未关闭
  • #143 来自 @datibbaw —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
64/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
技术栈
ruby
领域
networking

调研方向

从 IPAddr 类开始,检查现有的 IPv4、IPv6 和网络掩码处理。为提议的子网掩码常量添加覆盖测试,如果包含可选谓词,也为其 IPv4 和 IPv6 情况添加测试;完成标准是 CIDR 索引返回文档所述的掩码,并拒绝非连续掩码。

由索引模型根据 Issue 内容生成。

描述

Rationale: This SO question demonstrates there is at least some need to manipulate netmasks into CIDR integers. The accepted answer contains a bug which can be easily eliminated by hard coding the 2 sets of netmasks into the IPAddr class - see my alternative answer.

Proposed solution: Define IPAddr::IPV4_SUBNET_MASKS and IPAddr::IPV6_SUBNET_MASKS as follows:

class IPAddr
  IPV4_SUBNET_MASKS = (0..32).map { |n| IPAddr.new("0.0.0.0/#{n}").netmask }
  IPV6_SUBNET_MASKS = (0..128).map { |n| IPAddr.new("0::/#{n}").netmask }
end

# Now a user can do the following to get a subnet mask from a CIDR integer:
IPAddr::IPV4_SUBNET_MASKS[24]
# => "255.255.255.0"

IPAddr::IPV6_SUBNET_MASKS[64]
# => "ffff:ffff:ffff:ffff:0000:0000:0000:0000"

Optional: Add a predicate to check if an instance of IPAddr is a netmask:

class IPAddr
  def netmask?
    if ipv4?
      IPV4_SUBNET_MASKS.include? to_s
    else
      IPV6_SUBNET_MASKS.include? to_s
    end
  end
end

ipaddr1 = IPAddr.new '255.254.255.0'
ipaddr1.netmask? # => false

ipaddr2 = IPAddr.new '255.255.255.0/24'
ipaddr2.netmask? # => true

ipaddr3 = IPAddr.new '255.255.255.0'
ipaddr3.netmask? # => true

# And so on for IPv6 addresses
主要语言
Ruby
星标
83
派生
50
平均合并
1 天 2 小时
30 天内合并 PR
8

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ruby/ipaddr 的其他 Issue

查看 ruby/ipaddr 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。