solargraph 0.59.1 → 0.60.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/linting.yml +6 -0
- data/.github/workflows/plugins.yml +8 -0
- data/.github/workflows/rspec.yml +4 -1
- data/.github/workflows/typecheck.yml +2 -0
- data/.rubocop.yml +1 -0
- data/.rubocop_todo.yml +1 -1
- data/CHANGELOG.md +18 -0
- data/Gemfile +3 -0
- data/lib/solargraph/api_map/index.rb +13 -2
- data/lib/solargraph/api_map/store.rb +22 -8
- data/lib/solargraph/api_map.rb +38 -8
- data/lib/solargraph/complex_type/type_methods.rb +1 -0
- data/lib/solargraph/complex_type/unique_type.rb +16 -13
- data/lib/solargraph/complex_type.rb +5 -0
- data/lib/solargraph/convention/active_support_concern.rb +111 -111
- data/lib/solargraph/convention/base.rb +50 -50
- data/lib/solargraph/diagnostics.rb +55 -55
- data/lib/solargraph/doc_map.rb +1 -0
- data/lib/solargraph/environ.rb +52 -52
- data/lib/solargraph/gem_pins.rb +0 -11
- data/lib/solargraph/language_server/message/extended/environment.rb +25 -25
- data/lib/solargraph/language_server/message/initialized.rb +28 -28
- data/lib/solargraph/language_server/message/text_document.rb +28 -28
- data/lib/solargraph/language_server/progress.rb +143 -143
- data/lib/solargraph/language_server/transport/adapter.rb +68 -68
- data/lib/solargraph/language_server.rb +20 -20
- data/lib/solargraph/parser/parser_gem/node_chainer.rb +1 -0
- data/lib/solargraph/parser/parser_gem/node_methods.rb +42 -0
- data/lib/solargraph/parser/parser_gem/node_processors/alias_node.rb +24 -24
- data/lib/solargraph/parser/parser_gem/node_processors/casgn_node.rb +36 -36
- data/lib/solargraph/parser/parser_gem/node_processors/cvasgn_node.rb +24 -24
- data/lib/solargraph/parser/parser_gem/node_processors/gvasgn_node.rb +24 -24
- data/lib/solargraph/parser/parser_gem/node_processors/namespace_node.rb +29 -5
- data/lib/solargraph/parser/parser_gem/node_processors/sym_node.rb +20 -20
- data/lib/solargraph/pin/base.rb +31 -3
- data/lib/solargraph/pin/callable.rb +2 -2
- data/lib/solargraph/pin/common.rb +12 -0
- data/lib/solargraph/pin/method.rb +56 -16
- data/lib/solargraph/pin/reference/require.rb +14 -14
- data/lib/solargraph/pin/singleton.rb +11 -11
- data/lib/solargraph/rbs_map/conversions.rb +103 -145
- data/lib/solargraph/rbs_translator.rb +206 -0
- data/lib/solargraph/shell.rb +131 -64
- data/lib/solargraph/source/chain/array.rb +1 -12
- data/lib/solargraph/source/chain/block_symbol.rb +13 -13
- data/lib/solargraph/source/chain/block_variable.rb +13 -13
- data/lib/solargraph/source/chain/call.rb +8 -76
- data/lib/solargraph/source/chain/head.rb +19 -19
- data/lib/solargraph/source/chain/literal.rb +18 -14
- data/lib/solargraph/source/source_chainer.rb +4 -4
- data/lib/solargraph/source_map/mapper.rb +5 -135
- data/lib/solargraph/source_map.rb +14 -0
- data/lib/solargraph/version.rb +19 -1
- data/lib/solargraph/yard_map/cache.rb +25 -25
- data/lib/solargraph/yard_map/directives/attribute_directive.rb +65 -0
- data/lib/solargraph/yard_map/directives/domain_directive.rb +30 -0
- data/lib/solargraph/yard_map/directives/method_directive.rb +51 -0
- data/lib/solargraph/yard_map/directives/override_directive.rb +30 -0
- data/lib/solargraph/yard_map/directives/parse_directive.rb +53 -0
- data/lib/solargraph/yard_map/directives/visibility_directive.rb +70 -0
- data/lib/solargraph/yard_map/directives.rb +35 -0
- data/lib/solargraph/yard_map/macro.rb +113 -0
- data/lib/solargraph/yard_map/mapper/to_constant.rb +28 -28
- data/lib/solargraph/yard_map/mapper.rb +19 -1
- data/lib/solargraph/yard_map.rb +2 -0
- data/lib/solargraph.rb +1 -0
- data/solargraph.gemspec +1 -0
- metadata +24 -2
- data/rbs/fills/tuple/tuple.rbs +0 -177
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Solargraph
|
|
4
|
+
class YardMap
|
|
5
|
+
class Macro
|
|
6
|
+
PROCESSABLE_DIRECTIVES = %w[method attribute parse].freeze
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
# @param directive [YARD::Tags::Directive]
|
|
10
|
+
# @param method_pin [Pin::Method]
|
|
11
|
+
# @return [Macro]
|
|
12
|
+
def from_directive directive, method_pin
|
|
13
|
+
macro_name = directive.tag.name.empty? ? method_pin.path.downcase : directive.tag.name
|
|
14
|
+
method_object = method_object_from_pin(method_pin)
|
|
15
|
+
code = directive.tag.text.to_s.gsub(/\n(?!@!|\s)/, "\n ")
|
|
16
|
+
macro_object = YARD::CodeObjects::MacroObject.create(macro_name.to_s, code, method_object)
|
|
17
|
+
new(macro_object, method_pin, directive)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
# @param method_pin [Pin::Method]
|
|
23
|
+
# @return [YARD::CodeObjects::MethodObject]
|
|
24
|
+
def method_object_from_pin method_pin
|
|
25
|
+
namespace_object = nil
|
|
26
|
+
method_pin.each_closure do |namespace_pin|
|
|
27
|
+
next if namespace_pin.name.empty?
|
|
28
|
+
|
|
29
|
+
namespace_object = YARD::CodeObjects::NamespaceObject.new(
|
|
30
|
+
namespace_object,
|
|
31
|
+
namespace_pin.name.to_sym
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
# @sg-ignore Wrong argument type for YARD::CodeObjects::MethodObject.new: namespace
|
|
35
|
+
# expected YARD::CodeObjects::NamespaceObject, got nil.
|
|
36
|
+
# False positive because namespace_object is set in the loop above.
|
|
37
|
+
YARD::CodeObjects::MethodObject.new(namespace_object, method_pin.name)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @return [YARD::Tags::MacroDirective]
|
|
42
|
+
attr_reader :directive
|
|
43
|
+
# @return [YARD::CodeObjects::MacroObject]
|
|
44
|
+
attr_reader :macro_object
|
|
45
|
+
|
|
46
|
+
# @param macro_object [YARD::CodeObjects::MacroObject]
|
|
47
|
+
# @param method_pin [Pin::Method]
|
|
48
|
+
# @param directive [YARD::Tags::Directive]
|
|
49
|
+
def initialize macro_object, method_pin, directive
|
|
50
|
+
@macro_object = macro_object
|
|
51
|
+
@method_pin = method_pin
|
|
52
|
+
@directive = directive
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @return [String]
|
|
56
|
+
def name
|
|
57
|
+
@directive.tag.name.to_s
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @return [String]
|
|
61
|
+
def text
|
|
62
|
+
@directive.tag.text.to_s
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# @return [YARD::Tags::Tag]
|
|
66
|
+
def tag
|
|
67
|
+
@directive.tag
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @param chain [Source::Chain]
|
|
71
|
+
# @param pin [Pin::Closure]
|
|
72
|
+
# @param source_map [SourceMap]
|
|
73
|
+
# @return [Array<Pin::Base>]
|
|
74
|
+
def generate_pins_from chain, pin, source_map
|
|
75
|
+
call_location = Solargraph::Location.from_node(chain.node)
|
|
76
|
+
# @param generated_pins [Array<Pin::Base>]
|
|
77
|
+
generate_yardoc_from(chain, source_map).reduce([]) do |generated_pins, directive|
|
|
78
|
+
directive_processor = YardMap::Directives.for(directive)
|
|
79
|
+
next generated_pins unless directive_processor && call_location
|
|
80
|
+
generated_pins + directive_processor.process_directive(
|
|
81
|
+
source_map.source, source_map.pins, call_location.range.start, call_location.range.start, directive
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
# @param chain [Solargraph::Source::Chain]
|
|
89
|
+
# @param [SourceMap] source_map
|
|
90
|
+
# @return [Array<YARD::Tags::Directive>]
|
|
91
|
+
def generate_yardoc_from chain, source_map
|
|
92
|
+
name = chain.links.last.word
|
|
93
|
+
# @sg-ignore chain.links.last is assumed to be a Chain::Call
|
|
94
|
+
values = chain.links.last.arguments.map(&:node).map { |arg| Solargraph::Parser::ParserGem::NodeMethods.simple_convert(arg).to_s }
|
|
95
|
+
# @sg-ignore chain.node is assumed to exist
|
|
96
|
+
code = source_map.source.code_for(chain.node)
|
|
97
|
+
expanded_comment = macro_object.expand([name, *values], code)
|
|
98
|
+
.gsub(/\n(?!@!|\s)/, "\n ")
|
|
99
|
+
directives = Solargraph::Source.parse_docstring(expanded_comment).directives.select do |directive|
|
|
100
|
+
PROCESSABLE_DIRECTIVES.include?(directive.tag.tag_name)
|
|
101
|
+
end
|
|
102
|
+
directives.each do |directive|
|
|
103
|
+
# @sg-ignore chain.node is assumed to exist
|
|
104
|
+
comments = source_map.source.comments_for(chain.node)
|
|
105
|
+
if comments&.length&.positive? && directive.tag.tag_name != 'parse'
|
|
106
|
+
directive.tag.text += "\n#{comments}"
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
directives
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Solargraph
|
|
4
|
-
class YardMap
|
|
5
|
-
class Mapper
|
|
6
|
-
module ToConstant
|
|
7
|
-
extend YardMap::Helpers
|
|
8
|
-
|
|
9
|
-
# @param code_object [YARD::CodeObjects::Base]
|
|
10
|
-
# @param closure [Pin::Closure, nil]
|
|
11
|
-
# @param spec [Gem::Specification, nil]
|
|
12
|
-
# @return [Pin::Constant]
|
|
13
|
-
def self.make code_object, closure = nil, spec = nil
|
|
14
|
-
closure ||= create_closure_namespace_for(code_object, spec)
|
|
15
|
-
|
|
16
|
-
Pin::Constant.new(
|
|
17
|
-
location: object_location(code_object, spec),
|
|
18
|
-
closure: closure,
|
|
19
|
-
name: code_object.name.to_s,
|
|
20
|
-
comments: code_object.docstring ? code_object.docstring.all.to_s : '',
|
|
21
|
-
visibility: code_object.visibility,
|
|
22
|
-
source: :yardoc
|
|
23
|
-
)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Solargraph
|
|
4
|
+
class YardMap
|
|
5
|
+
class Mapper
|
|
6
|
+
module ToConstant
|
|
7
|
+
extend YardMap::Helpers
|
|
8
|
+
|
|
9
|
+
# @param code_object [YARD::CodeObjects::Base]
|
|
10
|
+
# @param closure [Pin::Closure, nil]
|
|
11
|
+
# @param spec [Gem::Specification, nil]
|
|
12
|
+
# @return [Pin::Constant]
|
|
13
|
+
def self.make code_object, closure = nil, spec = nil
|
|
14
|
+
closure ||= create_closure_namespace_for(code_object, spec)
|
|
15
|
+
|
|
16
|
+
Pin::Constant.new(
|
|
17
|
+
location: object_location(code_object, spec),
|
|
18
|
+
closure: closure,
|
|
19
|
+
name: code_object.name.to_s,
|
|
20
|
+
comments: code_object.docstring ? code_object.docstring.all.to_s : '',
|
|
21
|
+
visibility: code_object.visibility,
|
|
22
|
+
source: :yardoc
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -11,6 +11,7 @@ module Solargraph
|
|
|
11
11
|
# @param spec [Gem::Specification, nil]
|
|
12
12
|
def initialize code_objects, spec = nil
|
|
13
13
|
@code_objects = code_objects
|
|
14
|
+
@macro_code_objects = code_objects.select { |co| co.is_a?(YARD::CodeObjects::MacroObject) }
|
|
14
15
|
@spec = spec
|
|
15
16
|
# @type [Array<Solargraph::Pin::Base>]
|
|
16
17
|
@pins = []
|
|
@@ -24,7 +25,7 @@ module Solargraph
|
|
|
24
25
|
end
|
|
25
26
|
# Some yardocs contain documentation for dependencies that can be
|
|
26
27
|
# ignored here. The YardMap will load dependencies separately.
|
|
27
|
-
# @sg-ignore
|
|
28
|
+
# @sg-ignore does not consider `pin.location.nil? || ` condition
|
|
28
29
|
@pins.keep_if { |pin| pin.location.nil? || File.file?(pin.location.filename) } if @spec
|
|
29
30
|
@pins
|
|
30
31
|
end
|
|
@@ -65,6 +66,7 @@ module Solargraph
|
|
|
65
66
|
end
|
|
66
67
|
when YARD::CodeObjects::MethodObject
|
|
67
68
|
closure = @namespace_pins[code_object.namespace.to_s]
|
|
69
|
+
macros_for_method_object(code_object)
|
|
68
70
|
# @sg-ignore flow sensitive typing ought to be able to handle 'when ClassName'
|
|
69
71
|
if code_object.name == :initialize && code_object.scope == :instance
|
|
70
72
|
# @todo Check the visibility of <Class>.new
|
|
@@ -79,6 +81,22 @@ module Solargraph
|
|
|
79
81
|
end
|
|
80
82
|
result
|
|
81
83
|
end
|
|
84
|
+
|
|
85
|
+
# @return [Array<YARD::CodeObjects::MacroObject>]
|
|
86
|
+
def attached_macros
|
|
87
|
+
@attached_macros ||= @macro_code_objects.select(&:attached?)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# @return [Hash{YARD::CodeObjects::MethodObject => Array<YARD::CodeObjects::MacroObject>}]
|
|
91
|
+
def attached_macros_by_method_object
|
|
92
|
+
@attached_macros_by_method_object ||= attached_macros.group_by(&:method_object)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# @param method_object [YARD::CodeObjects::MethodObject]
|
|
96
|
+
# @return [Array<YARD::CodeObjects::MacroObject>]
|
|
97
|
+
def macros_for_method_object method_object
|
|
98
|
+
attached_macros_by_method_object[method_object]
|
|
99
|
+
end
|
|
82
100
|
end
|
|
83
101
|
end
|
|
84
102
|
end
|
data/lib/solargraph/yard_map.rb
CHANGED
|
@@ -13,5 +13,7 @@ module Solargraph
|
|
|
13
13
|
autoload :Cache, 'solargraph/yard_map/cache'
|
|
14
14
|
autoload :Mapper, 'solargraph/yard_map/mapper'
|
|
15
15
|
autoload :Helpers, 'solargraph/yard_map/helpers'
|
|
16
|
+
autoload :Macro, 'solargraph/yard_map/macro'
|
|
17
|
+
autoload :Directives, 'solargraph/yard_map/directives'
|
|
16
18
|
end
|
|
17
19
|
end
|
data/lib/solargraph.rb
CHANGED
|
@@ -49,6 +49,7 @@ module Solargraph
|
|
|
49
49
|
autoload :RbsMap, 'solargraph/rbs_map'
|
|
50
50
|
autoload :GemPins, 'solargraph/gem_pins'
|
|
51
51
|
autoload :PinCache, 'solargraph/pin_cache'
|
|
52
|
+
autoload :RbsTranslator, 'solargraph/rbs_translator'
|
|
52
53
|
|
|
53
54
|
dir = File.dirname(__FILE__)
|
|
54
55
|
VIEWS_PATH = File.join(dir, 'solargraph', 'views')
|
data/solargraph.gemspec
CHANGED
|
@@ -47,6 +47,7 @@ Gem::Specification.new do |s|
|
|
|
47
47
|
s.add_dependency 'rbs', '>= 3.10.0'
|
|
48
48
|
s.add_dependency 'reverse_markdown', '~> 3.0'
|
|
49
49
|
s.add_dependency 'rubocop', '~> 1.76'
|
|
50
|
+
s.add_dependency 'sord', '~> 7.0'
|
|
50
51
|
s.add_dependency 'thor', '~> 1.0'
|
|
51
52
|
s.add_dependency 'tilt', '~> 2.0'
|
|
52
53
|
s.add_dependency 'yard', '~> 0.9', '>= 0.9.24'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: solargraph
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.60.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Fred Snyder
|
|
@@ -253,6 +253,20 @@ dependencies:
|
|
|
253
253
|
- - "~>"
|
|
254
254
|
- !ruby/object:Gem::Version
|
|
255
255
|
version: '1.76'
|
|
256
|
+
- !ruby/object:Gem::Dependency
|
|
257
|
+
name: sord
|
|
258
|
+
requirement: !ruby/object:Gem::Requirement
|
|
259
|
+
requirements:
|
|
260
|
+
- - "~>"
|
|
261
|
+
- !ruby/object:Gem::Version
|
|
262
|
+
version: '7.0'
|
|
263
|
+
type: :runtime
|
|
264
|
+
prerelease: false
|
|
265
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
266
|
+
requirements:
|
|
267
|
+
- - "~>"
|
|
268
|
+
- !ruby/object:Gem::Version
|
|
269
|
+
version: '7.0'
|
|
256
270
|
- !ruby/object:Gem::Dependency
|
|
257
271
|
name: thor
|
|
258
272
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -742,6 +756,7 @@ files:
|
|
|
742
756
|
- lib/solargraph/rbs_map/core_fills.rb
|
|
743
757
|
- lib/solargraph/rbs_map/core_map.rb
|
|
744
758
|
- lib/solargraph/rbs_map/stdlib_map.rb
|
|
759
|
+
- lib/solargraph/rbs_translator.rb
|
|
745
760
|
- lib/solargraph/server_methods.rb
|
|
746
761
|
- lib/solargraph/shell.rb
|
|
747
762
|
- lib/solargraph/source.rb
|
|
@@ -790,7 +805,15 @@ files:
|
|
|
790
805
|
- lib/solargraph/workspace/require_paths.rb
|
|
791
806
|
- lib/solargraph/yard_map.rb
|
|
792
807
|
- lib/solargraph/yard_map/cache.rb
|
|
808
|
+
- lib/solargraph/yard_map/directives.rb
|
|
809
|
+
- lib/solargraph/yard_map/directives/attribute_directive.rb
|
|
810
|
+
- lib/solargraph/yard_map/directives/domain_directive.rb
|
|
811
|
+
- lib/solargraph/yard_map/directives/method_directive.rb
|
|
812
|
+
- lib/solargraph/yard_map/directives/override_directive.rb
|
|
813
|
+
- lib/solargraph/yard_map/directives/parse_directive.rb
|
|
814
|
+
- lib/solargraph/yard_map/directives/visibility_directive.rb
|
|
793
815
|
- lib/solargraph/yard_map/helpers.rb
|
|
816
|
+
- lib/solargraph/yard_map/macro.rb
|
|
794
817
|
- lib/solargraph/yard_map/mapper.rb
|
|
795
818
|
- lib/solargraph/yard_map/mapper/to_constant.rb
|
|
796
819
|
- lib/solargraph/yard_map/mapper/to_method.rb
|
|
@@ -804,7 +827,6 @@ files:
|
|
|
804
827
|
- rbs/fills/rubygems/0/errors.rbs
|
|
805
828
|
- rbs/fills/rubygems/0/spec_fetcher.rbs
|
|
806
829
|
- rbs/fills/rubygems/0/specification.rbs
|
|
807
|
-
- rbs/fills/tuple/tuple.rbs
|
|
808
830
|
- rbs/shims/ast/0/node.rbs
|
|
809
831
|
- rbs/shims/ast/2.4/.rbs_meta.yaml
|
|
810
832
|
- rbs/shims/ast/2.4/ast.rbs
|
data/rbs/fills/tuple/tuple.rbs
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
# <-- liberally borrowed from
|
|
2
|
-
# https://github.com/ruby/rbs/blob/master/core/array.rbs, which
|
|
3
|
-
# was generated from
|
|
4
|
-
# https://github.com/ruby/ruby/blob/master/array.c
|
|
5
|
-
# -->
|
|
6
|
-
module Solargraph
|
|
7
|
-
module Fills
|
|
8
|
-
class Tuple[unchecked out A,
|
|
9
|
-
unchecked out B = A,
|
|
10
|
-
unchecked out C = A | B,
|
|
11
|
-
unchecked out D = A | B | C,
|
|
12
|
-
unchecked out E = A | B | C | D,
|
|
13
|
-
unchecked out F = A | B | C | D | E,
|
|
14
|
-
unchecked out G = A | B | C | D | E | F,
|
|
15
|
-
unchecked out H = A | B | C | D | E | F | G,
|
|
16
|
-
unchecked out I = A | B | C | D | E | F | G | H,
|
|
17
|
-
unchecked out J = A | B | C | D | E | F | G | H | I] < Array[A | B | C | D | E | F | G | H | I | J]
|
|
18
|
-
# <!--
|
|
19
|
-
# rdoc-file=array.c
|
|
20
|
-
# - self[index] -> object or nil
|
|
21
|
-
# -->
|
|
22
|
-
# Returns elements from `self`; does not modify `self`.
|
|
23
|
-
#
|
|
24
|
-
# In brief:
|
|
25
|
-
#
|
|
26
|
-
# a = [:foo, 'bar', 2]
|
|
27
|
-
#
|
|
28
|
-
# # Single argument index: returns one element.
|
|
29
|
-
# a[0] # => :foo # Zero-based index.
|
|
30
|
-
#
|
|
31
|
-
# When a single integer argument `index` is given, returns the element at offset
|
|
32
|
-
# `index`:
|
|
33
|
-
#
|
|
34
|
-
# a = [:foo, 'bar', 2]
|
|
35
|
-
# a[0] # => :foo
|
|
36
|
-
# a[2] # => 2
|
|
37
|
-
# a # => [:foo, "bar", 2]
|
|
38
|
-
def []: (0 index) -> A
|
|
39
|
-
| (1 index) -> B
|
|
40
|
-
| (2 index) -> C
|
|
41
|
-
| (3 index) -> D
|
|
42
|
-
| (4 index) -> E
|
|
43
|
-
| (5 index) -> F
|
|
44
|
-
| (6 index) -> G
|
|
45
|
-
| (7 index) -> H
|
|
46
|
-
| (8 index) -> I
|
|
47
|
-
| (9 index) -> J
|
|
48
|
-
| (int index) -> nil
|
|
49
|
-
|
|
50
|
-
# <!--
|
|
51
|
-
# rdoc-file=array.c
|
|
52
|
-
# - at(index) -> object or nil
|
|
53
|
-
# -->
|
|
54
|
-
# Returns the element of `self` specified by the given `index` or `nil` if there
|
|
55
|
-
# is no such element; `index` must be an [integer-convertible
|
|
56
|
-
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
|
|
57
|
-
#
|
|
58
|
-
# For non-negative `index`, returns the element of `self` at offset `index`:
|
|
59
|
-
#
|
|
60
|
-
# a = [:foo, 'bar', 2]
|
|
61
|
-
# a.at(0) # => :foo
|
|
62
|
-
# a.at(2) # => 2
|
|
63
|
-
# a.at(2.0) # => 2
|
|
64
|
-
#
|
|
65
|
-
# Related: Array#[]; see also [Methods for
|
|
66
|
-
# Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
|
67
|
-
#
|
|
68
|
-
def at: (0 index) -> A
|
|
69
|
-
| (1 index) -> B
|
|
70
|
-
| (2 index) -> C
|
|
71
|
-
| (3 index) -> D
|
|
72
|
-
| (4 index) -> E
|
|
73
|
-
| (5 index) -> F
|
|
74
|
-
| (6 index) -> G
|
|
75
|
-
| (7 index) -> H
|
|
76
|
-
| (8 index) -> I
|
|
77
|
-
| (9 index) -> J
|
|
78
|
-
| (int index) -> nil
|
|
79
|
-
|
|
80
|
-
# <!--
|
|
81
|
-
# rdoc-file=array.c
|
|
82
|
-
# - fetch(index) -> element
|
|
83
|
-
# - fetch(index, default_value) -> element or default_value
|
|
84
|
-
# - fetch(index) {|index| ... } -> element or block_return_value
|
|
85
|
-
# -->
|
|
86
|
-
# Returns the element of `self` at offset `index` if `index` is in range;
|
|
87
|
-
# `index` must be an [integer-convertible
|
|
88
|
-
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
|
|
89
|
-
#
|
|
90
|
-
# With the single argument `index` and no block, returns the element at offset
|
|
91
|
-
# `index`:
|
|
92
|
-
#
|
|
93
|
-
# a = [:foo, 'bar', 2]
|
|
94
|
-
# a.fetch(1) # => "bar"
|
|
95
|
-
# a.fetch(1.1) # => "bar"
|
|
96
|
-
#
|
|
97
|
-
# With arguments `index` and `default_value` (which may be any object) and no
|
|
98
|
-
# block, returns `default_value` if `index` is out-of-range:
|
|
99
|
-
#
|
|
100
|
-
# a = [:foo, 'bar', 2]
|
|
101
|
-
# a.fetch(1, nil) # => "bar"
|
|
102
|
-
# a.fetch(3, :foo) # => :foo
|
|
103
|
-
#
|
|
104
|
-
# With argument `index` and a block, returns the element at offset `index` if
|
|
105
|
-
# index is in range (and the block is not called); otherwise calls the block
|
|
106
|
-
# with index and returns its return value:
|
|
107
|
-
#
|
|
108
|
-
# a = [:foo, 'bar', 2]
|
|
109
|
-
# a.fetch(1) {|index| raise 'Cannot happen' } # => "bar"
|
|
110
|
-
# a.fetch(50) {|index| "Value for #{index}" } # => "Value for 50"
|
|
111
|
-
#
|
|
112
|
-
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
|
113
|
-
#
|
|
114
|
-
def fetch: (0 index) -> A
|
|
115
|
-
| (1 index) -> B
|
|
116
|
-
| (2 index) -> C
|
|
117
|
-
| (3 index) -> D
|
|
118
|
-
| (4 index) -> E
|
|
119
|
-
| (5 index) -> F
|
|
120
|
-
| (6 index) -> G
|
|
121
|
-
| (7 index) -> H
|
|
122
|
-
| (8 index) -> I
|
|
123
|
-
| (9 index) -> J
|
|
124
|
-
| (int index) -> void
|
|
125
|
-
| [T] (0 index, T default) -> (A | T)
|
|
126
|
-
| [T] (1 index, T default) -> (B | T)
|
|
127
|
-
| [T] (2 index, T default) -> (C | T)
|
|
128
|
-
| [T] (3 index, T default) -> (D | T)
|
|
129
|
-
| [T] (4 index, T default) -> (E | T)
|
|
130
|
-
| [T] (5 index, T default) -> (F | T)
|
|
131
|
-
| [T] (6 index, T default) -> (G | T)
|
|
132
|
-
| [T] (7 index, T default) -> (H | T)
|
|
133
|
-
| [T] (8 index, T default) -> (I | T)
|
|
134
|
-
| [T] (9 index, T default) -> (J | T)
|
|
135
|
-
| [T] (int index, T default) -> (A | B | C | D | E | F | G | H | I | J | T)
|
|
136
|
-
| [T] (0 index) { (int index) -> T } -> (A | T)
|
|
137
|
-
| [T] (1 index) { (int index) -> T } -> (B | T)
|
|
138
|
-
| [T] (2 index) { (int index) -> T } -> (C | T)
|
|
139
|
-
| [T] (3 index) { (int index) -> T } -> (D | T)
|
|
140
|
-
| [T] (4 index) { (int index) -> T } -> (E | T)
|
|
141
|
-
| [T] (5 index) { (int index) -> T } -> (F | T)
|
|
142
|
-
| [T] (6 index) { (int index) -> T } -> (G | T)
|
|
143
|
-
| [T] (7 index) { (int index) -> T } -> (H | T)
|
|
144
|
-
| [T] (8 index) { (int index) -> T } -> (I | T)
|
|
145
|
-
| [T] (9 index) { (int index) -> T } -> (J | T)
|
|
146
|
-
| [T] (int index) { (int index) -> T } -> (A | B | C | D | E | F | G | H | I | J | T)
|
|
147
|
-
|
|
148
|
-
# <!--
|
|
149
|
-
# rdoc-file=array.rb
|
|
150
|
-
# - first -> object or nil
|
|
151
|
-
# - first(count) -> new_array
|
|
152
|
-
# -->
|
|
153
|
-
# Returns elements from `self`, or `nil`; does not modify `self`.
|
|
154
|
-
#
|
|
155
|
-
# With no argument given, returns the first element (if available):
|
|
156
|
-
#
|
|
157
|
-
# a = [:foo, 'bar', 2]
|
|
158
|
-
# a.first # => :foo
|
|
159
|
-
# a # => [:foo, "bar", 2]
|
|
160
|
-
#
|
|
161
|
-
# If `self` is empty, returns `nil`.
|
|
162
|
-
#
|
|
163
|
-
# [].first # => nil
|
|
164
|
-
#
|
|
165
|
-
# With a non-negative integer argument `count` given, returns the first `count`
|
|
166
|
-
# elements (as available) in a new array:
|
|
167
|
-
#
|
|
168
|
-
# a.first(0) # => []
|
|
169
|
-
# a.first(2) # => [:foo, "bar"]
|
|
170
|
-
# a.first(50) # => [:foo, "bar", 2]
|
|
171
|
-
#
|
|
172
|
-
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
|
173
|
-
#
|
|
174
|
-
def first: %a{implicitly-returns-nil} () -> A
|
|
175
|
-
end
|
|
176
|
-
end
|
|
177
|
-
end
|