Faker::Compass 使用指南:用 Ruby 生成罗盘方位、缩写与方位角数据

Faker::Compass 使用指南:用 Ruby 生成罗盘方位、缩写与方位角数据 Faker::Compass 使用指南用 Ruby 生成罗盘方位、缩写与方位角数据【免费下载链接】fakerA library for generating fake data such as names, addresses, and phone numbers.项目地址: https://gitcode.com/GitHub_Trending/fake/fakerFaker::Compass是 faker 库中专门用于生成方向类假数据的生成器模块自版本 1.8.0 起提供。它覆盖了从基础的四方位cardinal、八方位ordinal、十六方位half wind到完整的 32 方位quarter wind命名体系并同步提供对应的缩写abbreviation与方位角azimuth单位为度。在导航应用、风向展示、游戏地图、方位传感器模拟等场景中你可以用它快速产出符合海事罗盘命名规范、可复现的随机方向数据。读完本文你将掌握该模块的全部 16 个公开方法、底层数据组织方式locale YAML以及fetch/parse的取值原理。一、罗盘方位体系模块背后的命名规范在深入 API 之前先理解Faker::Compass所模拟的方位体系。传统罗盘把圆周划分为 32 个方位点point相邻两个方位之间相差 360° ÷ 32 11.25°。这 32 个点按层级分为四组Cardinal基本方位4 个north、east、south、west方位角 0°、90°、180°、270°以正北为 0°顺时针递增Ordinal / Intercardinal隅方位4 个northeast、southeast、southwest、northwest方位角 45°、135°、225°、315°Half wind半方位8 个如 north-northeast、east-northeast 等方位角为 22.5° 的奇数倍22.5°、67.5°、112.5° …Quarter wind四分之一方位16 个采用 north by east 这类「主方位 by 次方位」命名方位角为 11.25° 的奇数倍11.25°、33.75°、56.25° …。文档 doc/default/compass.md 中列出的全部方法正是围绕这一 32 点体系展开的。二、使用方法与完整 API 一览与 faker 的其他生成器一样使用前先引入库并确认版本require faker # 本模块自 faker 1.8.0 起可用在默认非懒加载模式下Faker::Compass随Faker::Default命名空间一起被加载若开启了懒加载首次访问Faker::Compass时会通过const_missing自动require对应文件见 lib/faker/default.rb 与 lib/faker.rb 中的lazy_load实现。模块共暴露 16 个公开方法按产出类型可分为三类完整名称word、缩写abbreviation和方位角azimuth。文档 doc/default/compass.md 中的完整示例及典型输出如下# —— 完整名称 —— Faker::Compass.direction # southeast 四类中随机一类 Faker::Compass.cardinal # north 基本方位 Faker::Compass.ordinal # northwest 隅方位 Faker::Compass.half_wind # north-northwest半方位连字符连接 Faker::Compass.quarter_wind # north by west 四分之一方位by 连接 # —— 缩写 —— Faker::Compass.abbreviation # NEbN 四类缩写中随机一类 Faker::Compass.cardinal_abbreviation # N Faker::Compass.ordinal_abbreviation # SW Faker::Compass.half_wind_abbreviation # NNE Faker::Compass.quarter_wind_abbreviation # SWbS # —— 方位角字符串形式的度数—— Faker::Compass.azimuth # 168.75 Faker::Compass.cardinal_azimuth # 90 Faker::Compass.ordinal_azimuth # 135 Faker::Compass.half_wind_azimuth # 292.5 Faker::Compass.quarter_wind_azimuth # 56.25需要说明的是方位角返回的是字符串如90、292.5而不是 Float 或 Integer使用时如需数值计算请先调用to_f。所有方法的返回类型在源码 lib/faker/default/compass.rb 的文档注释中均有return [String]标注。三、四类方位逐层解析与数据来源四个基础生成器分别对应 32 方位体系中的四个层级其数据全部来自英文 locale 文件 lib/locales/en/compass.yml层级方法词条数量命名特征Cardinalcardinal4单个单词north / east / south / westOrdinalordinal4单个单词northeast / southeast / southwest / northwestHalf windhalf_wind8双词连字符north-northeast、east-northeast、east-southeast、south-southeast、south-southwest、west-southwest、west-northwest、north-northwestQuarter windquarter_wind16主方位 by 次方位north by east、northeast by north、northeast by east、east by north、east by south、southeast by east、southeast by south、south by east、south by west、southwest by south、southwest by west、west by south、west by north、northwest by west、northwest by north、north by west例如quarter_wind只会从上面 16 个「by」格式词条中随机取一个绝不会产出 north by north 这类不符合规范的结果。从源码看这四个方法均通过fetch(compass.cardinal.word)、fetch(compass.ordinal.word)、fetch(compass.half-wind.word)、fetch(compass.quarter-wind.word)从 YAML 中随机取值见 lib/faker/default/compass.rb。四、缩写与方位角命名体系的两种「投影」除了完整名称模块还提供每个层级的缩写与方位角两套投影各有 4 个方法对应关系一一映射。4.1 缩写abbreviation海事惯例中32 个方位点均有标准缩写基本方位为单字母N/E/S/W隅方位为双字母NE/SE/SW/NW半方位为三字母NNE/ENE/ESE/SSE/SSW/WSW/WNW/NNW四分之一方位则用字母b表示 by如NbEnorth by east、SWbSsouthwest by south。测试文件 test/faker/default/test_faker_compass.rb 中用正则^[NEWS]?NEWS?$校验了缩写格式这正是对上述规则的形式化描述。4.2 方位角azimuth方位角以正北0°为基准顺时针度量。由 lib/locales/en/compass.yml 可见四组的数值集合cardinal_azimuth0、90、180、270ordinal_azimuth45、135、225、315half_wind_azimuth22.5、67.5、112.5、157.5、202.5、247.5、292.5、337.5quarter_wind_azimuth11.25、33.75、56.25、78.75、101.25、123.75、146.25、168.75、191.25、213.75、236.25、258.75、281.25、303.75、326.25、348.75。可以看到半方位是 45° 间隔再细分22.5° 奇数倍四分之一方位是 22.5° 间隔再细分11.25° 奇数倍相邻方位始终相差 11.25°与 32 点罗盘的定义完全一致。测试中的number_pattern /^\d(?:.\d\d?)?$/也验证了这些值可能是整数或带 12 位小数的字符串。五、组合方法direction / abbreviation / azimuth除按层级细分的 12 个方法外Faker::Compass还提供 3 个「汇总型」方法direction、abbreviation、azimuth。它们不直接对应某一层级而是先从四类中随机挑一个层级再返回该层级的随机值因此每次调用得到的粒度都可能不同。以 lib/locales/en/compass.yml 底部的模板定义为例direction: - #{cardinal} - #{ordinal} - #{half_wind} - #{quarter_wind} abbreviation: - #{cardinal_abbreviation} - #{ordinal_abbreviation} - #{half_wind_abbreviation} - #{quarter_wind_abbreviation} azimuth: - #{cardinal_azimuth} - #{ordinal_azimuth} - #{half_wind_azimuth} - #{quarter_wind_azimuth}这里用到了 faker 的字符串插值语法#{方法名}会被替换为调用对应方法的结果。底层机制见 lib/faker.rb 的parse方法——它先随机选出一条模板fetch再扫描其中的#{...}令牌若当前类拥有该方法则直接调用否则回退到 locale 翻译。因此direction可能在一次调用中返回 southcardinal另一次返回 north-northwesthalf wind。源码 lib/faker/default/compass.rb 中direction、abbreviation、azimuth三个方法正是通过parse实现的而其余 12 个方法使用fetch直接取值。5.1 32 方位完整对照表结合 lib/locales/en/compass.yml 的全部词条可以整理出完整的 32 点罗盘对照含名称、缩写、方位角方便在工程中直接参考类别名称缩写方位角(°)Cardinalnorth / east / south / westN / E / S / W0 / 90 / 180 / 270Ordinalnortheast / southeast / southwest / northwestNE / SE / SW / NW45 / 135 / 225 / 315Half windnorth-northeast … north-northwestNNE … NNW22.5 … 337.5步长 45Quarter windnorth by eastNbE11.25Quarter windnortheast by northNEbN33.75Quarter windnortheast by eastNEbE56.25Quarter windeast by northEbN78.75Quarter windeast by southEbS101.25Quarter windsoutheast by eastSEbE123.75Quarter windsoutheast by southSEbS146.25Quarter windsouth by eastSbE168.75Quarter windsouth by westSbW191.25Quarter windsouthwest by southSWbS213.75Quarter windsouthwest by westSWbW236.25Quarter windwest by southWbS258.75Quarter windwest by northWbN281.25Quarter windnorthwest by westNWbW303.75Quarter windnorthwest by northNWbN326.25Quarter windnorth by westNbW348.75六、多语言支持方位词条随 locale 切换Faker::Compass的数据来自 locale 文件因此切换Faker::Config.locale即可获得本地化方位词。仓库中至少以下 locale 定义了 compass 词条lib/locales/en/compass.yml完整 32 点体系含 quarter-wind 与四类 azimuthlib/locales/fr/compass.yml法语版含 cardinal / ordinal / half-wind 三类及 direction、abbreviation 组合模板注意法语中「西」的缩写是Oouest而非W且未提供 quarter-wind 层级lib/locales/de.yml、lib/locales/de-CH.yml、lib/locales/es-AR.yml、lib/locales/hy.yml德语含瑞士德语、阿根廷西班牙语、亚美尼亚语等 locale 也内嵌了 compass 词条德语词如 Norden、Osten、Süden、Westen。用法示例Faker::Config.locale fr Faker::Compass.cardinal # nord / est / sud / ouest Faker::Compass.half_wind # nord-nord-est 等法语半方位 Faker::Config.locale en # 用完记得切回多语言正确性由 locale 测试保障例如 test/test_fr_locale.rb 中test_fr_compass_methods用正则/^[NEOS]{1,3}$/校验法语缩写只含 N/E/O/S 四个字母而 test/test_de_locale.rb 则逐个断言德语环境下 16 个 compass 方法的返回均为字符串。如果你的自定义 locale 也需要 compass 数据参照 lib/locales/en/compass.yml 的键结构cardinal.word/abbreviation/azimuth、ordinal.*、half-wind.*、quarter-wind.*、direction、abbreviation、azimuth补充即可。七、测试与质量保障仓库为Faker::Compass提供了专项测试 test/faker/default/test_faker_compass.rb覆盖了每个公开方法的核心不变量cardinal、ordinal、half_wind均为单个单词\wquarter_wind必须是 word by word 形式^\w by \w$direction允许单单词、by 句式或连字符式^(?:\w|\w by \w|[\w-])$abbreviation符合缩写文法^[NEWS]?NEWS?$azimuth是整数或带 12 位小数的数值字符串^\d(?:.\d\d?)?$。这些测试同时印证了模块的随机性与格式稳定性若你 fork 后修改了 lib/locales/en/compass.yml 中的词条跑一遍该测试即可确认格式约束未被破坏。八、典型应用场景风向/航向数据脱敏在气象、物流、航海类系统中用Faker::Compass.direction或Faker::Compass.azimuth填充演示数据替代真实传感器读数游戏地图与 NPC 提示用quarter_wind如 north by west生成有真实感的寻路提示用half_wind表示细分方向UI 方向控件测试用cardinal_abbreviation、ordinal_abbreviation驱动 8 向/32 向摇杆或按钮组件的渲染测试方位-角度换算演示借助*_azimuth系列方法验证「方位名 ↔ 度数」的转换逻辑例如确认半方位292.5恰好落在西偏北方向。总结Faker::Compass用 16 个方法把 32 点罗盘的名称、缩写与方位角三套表示完整覆盖并通过 locale 文件实现了多语言可定制。理解其「层级词条 组合模板」的设计fetch直取、parse插值你就能轻松地把方向类假数据集成到自己的 Ruby 项目中需要自定义方向集时只需参照 lib/locales/en/compass.yml 的结构扩展 locale 词条即可。更多生成器的通用机制可参考 GENERATORS.md。【免费下载链接】fakerA library for generating fake data such as names, addresses, and phone numbers.项目地址: https://gitcode.com/GitHub_Trending/fake/faker创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考