Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
10c18bd
bundle exec bin/sort stdlib/builtin/symbol.rbs
hanachin Jan 8, 2020
a057327
bundle exec exe/rbs prototype runtime --merge Symbol
hanachin Jan 8, 2020
3a20101
bundle exec bin/annotate-with-rdoc stdlib/builtin/symbol.rbs
hanachin Jan 8, 2020
5dbbc0d
Sort SymbolTest test methods
hanachin Jan 8, 2020
f28e42d
Add test for Symbol.all_symbols
hanachin Jan 8, 2020
9ee2df9
Update Symbol#<=>
hanachin Jan 8, 2020
47aad51
Add test for Symbol#==
hanachin Jan 8, 2020
26d35eb
Update Symbol#===
hanachin Jan 8, 2020
450223d
Update Symbol#=~
hanachin Jan 8, 2020
86cc449
Update Symbol#[]
hanachin Jan 9, 2020
8b6e590
Update Symbol#capitalize
hanachin Jan 9, 2020
705cc44
Update Symbol#casecmp
hanachin Jan 10, 2020
edd4ec2
Update Symbol#casecmp?
hanachin Jan 10, 2020
074fac5
Update Symbol#downcase
hanachin Jan 10, 2020
58f9d6d
Add test for Symbol#empty?
hanachin Jan 10, 2020
4e48ec9
Add test for Symbol#encoding
hanachin Jan 10, 2020
7682c50
Update Symbol#end_with?
hanachin Jan 10, 2020
e72518f
Add test for Symbol#id2name
hanachin Jan 10, 2020
711115f
Add test for Symbol#inspect
hanachin Jan 10, 2020
7507641
Add test for Symbol#intern
hanachin Jan 10, 2020
8016c9f
Add test for Symbol#length
hanachin Jan 10, 2020
5d14070
Update Symbol#match
hanachin Jan 10, 2020
4c3a803
Update Symbol#match?
hanachin Jan 10, 2020
ebc2cdb
Add test for Symbol#next
hanachin Jan 10, 2020
419061a
Update Symbol#size
hanachin Jan 10, 2020
65812af
Update Symbol#slice
hanachin Jan 10, 2020
ed0d460
Update Symbol#start_with?
hanachin Jan 10, 2020
71dc0a6
Update Symbol#succ
hanachin Jan 10, 2020
f74cc9a
Update Symbol#swapcase
hanachin Jan 10, 2020
920ce60
Add test for Symbol#to_proc
hanachin Jan 10, 2020
f01c1f6
Update Symbol#to_sym
hanachin Jan 10, 2020
087fc0f
Update for Symbol#upcase
hanachin Jan 10, 2020
8fbf642
Update rubocop TargetRubyVersion 2.6 -> 2.7
hanachin Jan 10, 2020
a270cb3
Remove wrong comment
hanachin Jan 13, 2020
5fe3f40
Update Symbol#to_s
hanachin Jan 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require: rubocop-rubycw
AllCops:
TargetRubyVersion: 2.6
TargetRubyVersion: 2.7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

DisabledByDefault: true
Exclude:
- 'vendor/bundle/**/*'
Expand Down
249 changes: 172 additions & 77 deletions stdlib/builtin/symbol.rbs
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
# `Symbol` objects represent names and some strings inside the Ruby
# interpreter. They are generated using the `:name` and `:"string"`
# literals syntax, and by the various `to_sym` methods. The same `Symbol`
# object will be created for a given name or string for the duration of a
# program's execution, regardless of the context or meaning of that name.
# Thus if `Fred` is a constant in one context, a method in another, and a
# class in a third, the `Symbol` `:Fred` will be the same object in all
# Symbol objects represent names inside the Ruby interpreter. They are generated
# using the `:name` and `:"string"` literals syntax, and by the various `to_sym`
# methods. The same Symbol object will be created for a given name or string for
# the duration of a program's execution, regardless of the context or meaning of
# that name. Thus if `Fred` is a constant in one context, a method in another,
# and a class in a third, the Symbol `:Fred` will be the same object in all
# three contexts.
#
# ```ruby
# module One
# class Fred
# end
# $f1 = :Fred
# end
# module Two
# Fred = 1
# $f2 = :Fred
# end
# def Fred()
# end
# $f3 = :Fred
# $f1.object_id #=> 2514190
# $f2.object_id #=> 2514190
# $f3.object_id #=> 2514190
# ```
class Symbol < Object
# module One
# class Fred
# end
# $f1 = :Fred
# end
# module Two
# Fred = 1
# $f2 = :Fred
# end
# def Fred()
# end
# $f3 = :Fred
# $f1.object_id #=> 2514190
# $f2.object_id #=> 2514190
# $f3.object_id #=> 2514190
#
class Symbol
include Comparable

# Returns an array of all the symbols currently in Ruby's symbol table.
Expand All @@ -36,99 +34,196 @@ class Symbol < Object
# :default_proc, :compact, :extend,
# :Tms, :getwd, :$=, :ThreadGroup,
# :wait2, :$>]
#
def self.all_symbols: () -> ::Array[Symbol]

def <=>: (Symbol other) -> Integer?
public

# Compares `symbol` with `other_symbol` after calling #to_s on each of the
# symbols. Returns -1, 0, +1, or `nil` depending on whether `symbol` is less
# than, equal to, or greater than `other_symbol`.
#
# `nil` is returned if the two values are incomparable.
#
# See String#<=> for more information.
#
def <=>: (Symbol other) -> Integer
| (untyped other) -> Integer?

# Equality---If *sym* and *obj* are exactly the same symbol, returns `true`.
#
def ==: (untyped obj) -> bool

def =~: (untyped obj) -> Integer?
# Equality---If *sym* and *obj* are exactly the same symbol, returns `true`.
#
def ===: (untyped obj) -> bool

def []: (Integer idx_or_range) -> String
| (Integer idx_or_range, ?Integer n) -> String
| (::Range[Integer] idx_or_range) -> String
# Returns `sym.to_s =~ obj`.
#
def =~: (untyped obj) -> Integer?

# Same as `sym.to_s.capitalize.intern` .
# Returns `sym.to_s[]`.
#
def []: (Integer index) -> String?
| (Integer start, Integer length) -> String?
| (Range[Integer] range) -> String?
| (Regexp regexp) -> String?
| (Regexp regexp, Integer | String capture) -> String?
| (String match_str) -> String?

# Same as `sym.to_s.capitalize.intern`.
#
def capitalize: () -> Symbol
| (:ascii | :lithuanian | :turkic) -> Symbol
| (:lithuanian, :turkic) -> Symbol
| (:turkic, :lithuanian) -> Symbol

# Case-insensitive version of Symbol#<=>. Currently, case-insensitivity only
# works on characters A-Z/a-z, not all of Unicode. This is different from
# Symbol#casecmp?.
#
# :aBcDeF.casecmp(:abcde) #=> 1
# :aBcDeF.casecmp(:abcdef) #=> 0
# :aBcDeF.casecmp(:abcdefg) #=> -1
# :abcdef.casecmp(:ABCDEF) #=> 0
#
# `nil` is returned if the two symbols have incompatible encodings, or if
# `other_symbol` is not a symbol.
#
# :foo.casecmp(2) #=> nil
# "\u{e4 f6 fc}".encode("ISO-8859-1").to_sym.casecmp(:"\u{c4 d6 dc}") #=> nil
#
def casecmp: (Symbol other) -> Integer?
| (untyped other) -> Integer?

def casecmp?: (Symbol other) -> bool?
# Returns `true` if `sym` and `other_symbol` are equal after Unicode case
# folding, `false` if they are not equal.
#
# :aBcDeF.casecmp?(:abcde) #=> false
# :aBcDeF.casecmp?(:abcdef) #=> true
# :aBcDeF.casecmp?(:abcdefg) #=> false
# :abcdef.casecmp?(:ABCDEF) #=> true
# :"\u{e4 f6 fc}".casecmp?(:"\u{c4 d6 dc}") #=> true
#
# `nil` is returned if the two symbols have incompatible encodings, or if
# `other_symbol` is not a symbol.
#
# :foo.casecmp?(2) #=> nil
# "\u{e4 f6 fc}".encode("ISO-8859-1").to_sym.casecmp?(:"\u{c4 d6 dc}") #=> nil
#
def casecmp?: (Symbol other) -> bool

# Same as `sym.to_s.downcase.intern` .
# Same as `sym.to_s.downcase.intern`.
#
def downcase: () -> Symbol
| (:ascii | :fold | :lithuanian | :turkic) -> Symbol
| (:lithuanian, :turkic) -> Symbol
| (:turkic, :lithuanian) -> Symbol

# Returns whether *sym* is :“” or not.
# Returns whether *sym* is :"" or not.
#
def empty?: () -> bool

# Returns the [Encoding](https://ruby-doc.org/core-2.6.3/Encoding.html)
# object that represents the encoding of *sym* .
# Returns the Encoding object that represents the encoding of *sym*.
#
def encoding: () -> Encoding

# Returns the name or string corresponding to *sym* .
# Returns true if `sym` ends with one of the `suffixes` given.
#
# :hello.end_with?("ello") #=> true
#
# # returns true if one of the +suffixes+ matches.
# :hello.end_with?("heaven", "ello") #=> true
# :hello.end_with?("heaven", "paradise") #=> false
#
def end_with?: (String str, *String str) -> bool

# Returns the name or string corresponding to *sym*.
#
# :fred.id2name #=> "fred"
# :ginger.to_s #=> "ginger"
#
# ```ruby
# :fred.id2name #=> "fred"
# :ginger.to_s #=> "ginger"
# ```
def id2name: () -> String

# Returns the representation of *sym* as a symbol literal.
#
# ```ruby
# :fred.inspect #=> ":fred"
# ```
# :fred.inspect #=> ":fred"
#
def inspect: () -> String

# In general, `to_sym` returns the `Symbol` corresponding to an object. As
# *sym* is already a symbol, `self` is returned in this case.
# In general, `to_sym` returns the Symbol corresponding to an object. As *sym*
# is already a symbol, `self` is returned in this case.
#
def intern: () -> self

# Same as `sym.to_s.length` .
# Same as `sym.to_s.length`.
#
def length: () -> Integer

def match: (untyped obj) -> Integer?
# Returns `sym.to_s.match`.
#
def match: (Regexp | String pattern, ?Integer pos) ?{ (MatchData) -> void } -> MatchData?

def match?: (*untyped args) -> untyped
# Returns `sym.to_s.match?`.
#
def match?: (Regexp | String pattern, ?Integer pos) -> bool

# Same as `sym.to_s.succ.intern` .
# Same as `sym.to_s.succ.intern`.
#
def next: () -> Symbol

# Same as `sym.to_s.succ.intern` .
def succ: () -> Symbol
# Same as `sym.to_s.length`.
#
alias size length

# Same as `sym.to_s.swapcase.intern` .
def swapcase: () -> Symbol
# Returns `sym.to_s[]`.
#
alias slice `[]`

# Returns a *Proc* object which responds to the given method by *sym* .
# Returns true if `sym` starts with one of the `prefixes` given. Each of the
# `prefixes` should be a String or a Regexp.
#
# ```ruby
# (1..3).collect(&:to_s) #=> ["1", "2", "3"]
# ```
def to_proc: () -> Proc
# :hello.start_with?("hell") #=> true
# :hello.start_with?(/H/i) #=> true
#
# # returns true if one of the prefixes matches.
# :hello.start_with?("heaven", "hell") #=> true
# :hello.start_with?("heaven", "paradise") #=> false
def start_with?: (String str, *String str) -> bool

# Same as `sym.to_s.upcase.intern` .
def upcase: () -> Symbol
# Same as `sym.to_s.succ.intern`.
#
alias succ next

# Same as `sym.to_s.length` .
def size: () -> Integer
# Same as `sym.to_s.swapcase.intern`.
#
def swapcase: () -> Symbol
| (:ascii | :lithuanian | :turkic) -> Symbol
| (:lithuanian, :turkic) -> Symbol
| (:turkic, :lithuanian) -> Symbol

def slice: (Integer idx_or_range) -> String
| (Integer idx_or_range, ?Integer n) -> String
| (::Range[Integer] idx_or_range) -> String
# Returns a *Proc* object which responds to the given method by *sym*.
#
# (1..3).collect(&:to_s) #=> ["1", "2", "3"]
#
def to_proc: () -> Proc

# Returns the name or string corresponding to *sym* .
# Returns the name or string corresponding to *sym*.
#
# :fred.id2name #=> "fred"
# :ginger.to_s #=> "ginger"
#
# ```ruby
# :fred.id2name #=> "fred"
# :ginger.to_s #=> "ginger"
# ```
def to_s: () -> String
alias to_s id2name

# In general, `to_sym` returns the `Symbol` corresponding to an object. As
# *sym* is already a symbol, `self` is returned in this case.
def to_sym: () -> self
# In general, `to_sym` returns the Symbol corresponding to an object. As *sym*
# is already a symbol, `self` is returned in this case.
#
alias to_sym intern

def start_with?: (String str) -> bool
def end_with?: (String str) -> bool
# Same as `sym.to_s.upcase.intern`.
#
def upcase: () -> Symbol
| (:ascii | :lithuanian | :turkic) -> Symbol
| (:lithuanian, :turkic) -> Symbol
| (:turkic, :lithuanian) -> Symbol
end
Loading