Skip to content

Commit

Permalink
Move abbrev from rbs/stdlib
Browse files Browse the repository at this point in the history
See also:
- ruby/rbs#2258
  • Loading branch information
ksss committed Feb 2, 2025
1 parent fde297c commit c455ec4
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
19 changes: 19 additions & 0 deletions gems/abbrev/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This configuration inherits from /.rubocop.yml.
# You can configure RBS style of this gem.
# This file is used on CI. It is configured to automatically
# make rubocop suggestions on pull requests for this gem.
# If you do not like the style enforcement, you should remove this file.
inherit_from: ../../.rubocop.yml

##
# If you want to customize the style, please consult with the gem reviewers.
# You can see the list of cops at https://github.com/ksss/rubocop-on-rbs/blob/main/docs/modules/ROOT/pages/cops.adoc

RBS/Layout:
Enabled: true

RBS/Lint:
Enabled: true

RBS/Style:
Enabled: true
10 changes: 10 additions & 0 deletions gems/abbrev/0.1/_test/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Write Ruby code to test the RBS.
# It is type checked by `steep check` command.

require "abbrev"

Abbrev.abbrev([])
Abbrev.abbrev(%w(abbrev))
Abbrev.abbrev(%w(abbrev), 'abb')
Abbrev.abbrev(%w(abbrev), /^abb/)
Abbrev.abbrev(%w(abbrev), nil)
66 changes: 66 additions & 0 deletions gems/abbrev/0.1/abbrev.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# <!-- rdoc-file=lib/abbrev.rb -->
# Calculates the set of unambiguous abbreviations for a given set of strings.
#
# require 'abbrev'
# require 'pp'
#
# pp Abbrev.abbrev(['ruby'])
# #=> {"ruby"=>"ruby", "rub"=>"ruby", "ru"=>"ruby", "r"=>"ruby"}
#
# pp Abbrev.abbrev(%w{ ruby rules })
#
# *Generates:*
# { "ruby" => "ruby",
# "rub" => "ruby",
# "rules" => "rules",
# "rule" => "rules",
# "rul" => "rules" }
#
# It also provides an array core extension, Array#abbrev.
#
# pp %w{ summer winter }.abbrev
#
# *Generates:*
# { "summer" => "summer",
# "summe" => "summer",
# "summ" => "summer",
# "sum" => "summer",
# "su" => "summer",
# "s" => "summer",
# "winter" => "winter",
# "winte" => "winter",
# "wint" => "winter",
# "win" => "winter",
# "wi" => "winter",
# "w" => "winter" }
#
module Abbrev
# <!--
# rdoc-file=lib/abbrev.rb
# - abbrev(words, pattern = nil)
# -->
# Given a set of strings, calculate the set of unambiguous abbreviations for
# those strings, and return a hash where the keys are all the possible
# abbreviations and the values are the full strings.
#
# Thus, given `words` is "car" and "cone", the keys pointing to "car" would be
# "ca" and "car", while those pointing to "cone" would be "co", "con", and
# "cone".
#
# require 'abbrev'
#
# Abbrev.abbrev(%w{ car cone })
# #=> {"ca"=>"car", "con"=>"cone", "co"=>"cone", "car"=>"car", "cone"=>"cone"}
#
# The optional `pattern` parameter is a pattern or a string. Only input strings
# that match the pattern or start with the string are included in the output
# hash.
#
# Abbrev.abbrev(%w{car box cone crab}, /b/)
# #=> {"box"=>"box", "bo"=>"box", "b"=>"box", "crab" => "crab"}
#
# Abbrev.abbrev(%w{car box cone}, 'ca')
# #=> {"car"=>"car", "ca"=>"car"}
#
def self?.abbrev: (Array[String], ?String | Regexp | nil) -> Hash[String, String]
end
26 changes: 26 additions & 0 deletions gems/abbrev/0.1/array.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%a{annotate:rdoc:skip}
class Array[unchecked out Elem]
# <!--
# rdoc-file=lib/abbrev.rb
# - abbrev(pattern = nil)
# -->
# Calculates the set of unambiguous abbreviations for the strings in `self`.
#
# require 'abbrev'
# %w{ car cone }.abbrev
# #=> {"car"=>"car", "ca"=>"car", "cone"=>"cone", "con"=>"cone", "co"=>"cone"}
#
# The optional `pattern` parameter is a pattern or a string. Only input strings
# that match the pattern or start with the string are included in the output
# hash.
#
# %w{ fast boat day }.abbrev(/^.a/)
# #=> {"fast"=>"fast", "fas"=>"fast", "fa"=>"fast", "day"=>"day", "da"=>"day"}
#
# Abbrev.abbrev(%w{car box cone}, "ca")
# #=> {"car"=>"car", "ca"=>"car"}
#
# See also Abbrev.abbrev
#
def abbrev: (?String | Regexp | nil) -> Hash[String, String]
end
2 changes: 2 additions & 0 deletions gems/abbrev/_reviewers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reviewers:
- ksss

0 comments on commit c455ec4

Please sign in to comment.