Skip to content

Generated from Sorbet RBI #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
978 changes: 807 additions & 171 deletions stdlib/builtin/array.rbi

Large diffs are not rendered by default.

108 changes: 95 additions & 13 deletions stdlib/builtin/basic_object.rbi
Original file line number Diff line number Diff line change
@@ -1,18 +1,100 @@
# [BasicObject](BasicObject) is the parent class of
# all classes in Ruby. It's an explicit blank class.
#
# [BasicObject](BasicObject) can be used for creating
# object hierarchies independent of Ruby's object hierarchy, proxy objects
# like the Delegator class, or other uses where namespace pollution from
# Ruby's methods and classes must be avoided.
#
# To avoid polluting [BasicObject](BasicObject) for
# other users an appropriately named subclass of
# [BasicObject](BasicObject) should be created instead
# of directly modifying BasicObject:
#
# ```ruby
# class MyObjectSystem < BasicObject
# end
# ```
#
# [BasicObject](BasicObject) does not include
# [Kernel](https://ruby-doc.org/core-2.6.3/Kernel.html) (for methods like
# `puts` ) and [BasicObject](BasicObject) is outside
# of the namespace of the standard library so common classes will not be
# found without using a full class path.
#
# A variety of strategies can be used to provide useful portions of the
# standard library to subclasses of
# [BasicObject](BasicObject). A subclass could
# `include Kernel` to obtain `puts`, `exit`, etc. A custom Kernel-like
# module could be created and included or delegation can be used via
# [method\_missing](BasicObject#method-i-method_missing)
# :
#
# ```ruby
# class MyObjectSystem < BasicObject
# DELEGATE = [:puts, :p]
#
# def method_missing(name, *args, &block)
# super unless DELEGATE.include? name
# ::Kernel.send(name, *args, &block)
# end
#
# def respond_to_missing?(name, include_private = false)
# DELEGATE.include?(name) or super
# end
# end
# ```
#
# Access to classes and modules from the Ruby standard library can be
# obtained in a [BasicObject](BasicObject) subclass by
# referencing the desired constant from the root like `::File` or
# `::Enumerator` . Like
# [method\_missing](BasicObject#method-i-method_missing)
# , const\_missing can be used to delegate constant lookup to `Object` :
#
# ```ruby
# class MyObjectSystem < BasicObject
# def self.const_missing(name)
# ::Object.const_get(name)
# end
# end
# ```
class BasicObject
def initialize: -> void
# Boolean negate.
def !: () -> bool
def `!=`: (any) -> bool
def __id__: -> Integer
def __send__: (*any) -> any
def equal?: (any) -> bool
def instance_eval: (String, ?String filename, ?Integer lineno) -> any
| [X] { (self) -> X } -> X
def instance_exec: [X] (*any) { (*any) -> X } -> X

private
def !=: (any other) -> bool

def ==: (any other) -> bool

# Returns an integer identifier for `obj` .
#
# The same number will be returned on all calls to `object_id` for a given
# object, and no two active objects will share an id.
#
# Note: that some objects of builtin classes are reused for optimization.
# This is the case for immediate values and frozen string literals.
#
# Immediate values are not passed by reference but are passed by value:
# `nil`, `true`, `false`, Fixnums, Symbols, and some Floats.
#
# ```ruby
# Object.new.object_id == Object.new.object_id # => false
# (21 * 2).object_id == (21 * 2).object_id # => true
# "hello".object_id == "hello".object_id # => false
# "hi".freeze.object_id == "hi".freeze.object_id # => true
# ```
def __id__: () -> Integer

def __send__: (Symbol arg0, *any arg1) -> any

def method_missing: (Symbol, *any) -> any
def singleton_method_added: (Symbol) -> void
def singleton_method_removed: (Symbol) -> void
def singleton_method_undefined: (Symbol) -> void
def equal?: (any other) -> bool

def instance_eval: (?String arg0, ?String filename, ?Integer lineno) -> any
| [U] () { () -> U } -> U

def instance_exec: [U, V] (*V args) { (any args) -> U } -> U

private
def initialize: () -> void
end
100 changes: 100 additions & 0 deletions stdlib/builtin/binding.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Objects of class `Binding` encapsulate the execution context at some
# particular place in the code and retain this context for future use. The
# variables, methods, value of `self`, and possibly an iterator block
# that can be accessed in this context are all retained.
# [Binding](Binding) objects can be created using
# `Kernel#binding`, and are made available to the callback of
# `Kernel#set_trace_func`.
#
# These binding objects can be passed as the second argument of the
# `Kernel#eval` method, establishing an environment for the evaluation.
#
# ```ruby
# class Demo
# def initialize(n)
# @secret = n
# end
# def get_binding
# binding
# end
# end
#
# k1 = Demo.new(99)
# b1 = k1.get_binding
# k2 = Demo.new(-3)
# b2 = k2.get_binding
#
# eval("@secret", b1) #=> 99
# eval("@secret", b2) #=> -3
# eval("@secret") #=> nil
# ```
#
# [Binding](Binding) objects have no class-specific
# methods.
class Binding < Object
# Returns `true` if a local variable `symbol` exists.
#
# ```ruby
# def foo
# a = 1
# binding.local_variable_defined?(:a) #=> true
# binding.local_variable_defined?(:b) #=> false
# end
# ```
#
# This method is the short version of the following code:
#
# ```ruby
# binding.eval("defined?(#{symbol}) == 'local-variable'")
# ```
def local_variable_defined?: (String | Symbol symbol) -> bool

# Returns the value of the local variable `symbol`.
#
# ```ruby
# def foo
# a = 1
# binding.local_variable_get(:a) #=> 1
# binding.local_variable_get(:b) #=> NameError
# end
# ```
#
# This method is the short version of the following code:
#
# ```ruby
# binding.eval("#{symbol}")
# ```
def local_variable_get: (String | Symbol symbol) -> any

# Set local variable named `symbol` as `obj`.
#
# ```ruby
# def foo
# a = 1
# bind = binding
# bind.local_variable_set(:a, 2) # set existing local variable `a'
# bind.local_variable_set(:b, 3) # create new local variable `b'
# # `b' exists only in binding
#
# p bind.local_variable_get(:a) #=> 2
# p bind.local_variable_get(:b) #=> 3
# p a #=> 2
# p b #=> NameError
# end
# ```
#
# This method behaves similarly to the following code:
#
# ```ruby
# binding.eval("#{symbol} = #{obj}")
# ```
#
# if `obj` can be dumped in Ruby code.
def local_variable_set: (String | Symbol symbol, any obj) -> any

# Returns the bound receiver of the binding object.
def receiver: () -> Object

# Returns the Ruby source filename and line number of the binding object.
def source_location: () -> [ String, Integer ]
end
14 changes: 5 additions & 9 deletions stdlib/builtin/builtin.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ interface _ToS
def to_s: -> String
end

interface _Each[A, B]
def each: { (A) -> void } -> B
end

class TrueClass
def !: -> bool
interface _ToStr
def to_str: () -> String
end

class FalseClass
def !: -> bool
interface _Each[A, B]
def each: { (A) -> void } -> B
end

class NilClass
class BigDecimal
end
124 changes: 120 additions & 4 deletions stdlib/builtin/class.rbi
Original file line number Diff line number Diff line change
@@ -1,6 +1,122 @@
# Classes in Ruby are first-class objects---each is an instance of class
# `Class` .
#
# Typically, you create a new class by using:
#
# ```ruby
# class Name
# # some code describing the class behavior
# end
# ```
#
# When a new class is created, an object of type
# [Class](Class) is initialized and assigned to a
# global constant ( `Name` in this case).
#
# When `Name.new` is called to create a new object, the `new` method in
# `Class` is run by default. This can be demonstrated by overriding `new`
# in `Class` :
#
# ```ruby
# class Class
# alias old_new new
# def new(*args)
# print "Creating a new ", self.name, "\n"
# old_new(*args)
# end
# end
#
# class Name
# end
#
# n = Name.new
# ```
#
# *produces:*
#
# ```ruby
# Creating a new Name
# ```
#
# Classes, modules, and objects are interrelated. In the diagram that
# follows, the vertical arrows represent inheritance, and the parentheses
# metaclasses. All metaclasses are instances of the class \`Class'.
#
# ```
# +---------+ +-...
# | | |
# BasicObject-----|-->(BasicObject)-------|-...
# ^ | ^ |
# | | | |
# Object---------|----->(Object)---------|-...
# ^ | ^ |
# | | | |
# +-------+ | +--------+ |
# | | | | | |
# | Module-|---------|--->(Module)-|-...
# | ^ | | ^ |
# | | | | | |
# | Class-|---------|---->(Class)-|-...
# | ^ | | ^ |
# | +---+ | +----+
# | |
# obj--->OtherClass---------->(OtherClass)-----------...
# ```
class Class < Module
def initialize: (?Class) -> void
def new: (*any) -> any
def class: -> singleton(Class)
def allocate: -> any
# Allocates space for a new object of *class* ’s class and does not call
# initialize on the new instance. The returned object must be an instance
# of *class* .
#
# ```ruby
# klass = Class.new do
# def initialize(*args)
# @initialized = true
# end
#
# def initialized?
# @initialized || false
# end
# end
#
# klass.allocate.initialized? #=> false
# ```
def allocate: () -> any

# Sorbet hijacks Class#new to re-use the sig from MyClass#initialize when creating new instances of a class.
# This method must be here so that all calls to MyClass.new aren't forced to take 0 arguments.
# Calls `allocate` to create a new object of *class* ’s class, then
# invokes that object’s `initialize` method, passing it *args* . This is
# the method that ends up getting called whenever an object is constructed
# using .new.
def new: (*any args) -> any

def inherited: (Class arg0) -> any

def instance_methods: (?bool arg0) -> ::Array[Symbol]

def name: () -> String?

# Returns the superclass of *class* , or `nil` .
#
# ```ruby
# File.superclass #=> IO
# IO.superclass #=> Object
# Object.superclass #=> BasicObject
# class Foo; end
# class Bar < Foo; end
# Bar.superclass #=> Foo
# ```
#
# Returns nil when the given class does not have a parent class:
#
# ```ruby
# BasicObject.superclass #=> nil
# ```
def `superclass`: () -> Class?
| () -> Class

def initialize: () -> void
| (?Class superclass) -> void
| () { (Class arg0) -> any } -> void
| (?Class superclass) { (Class arg0) -> any } -> void
end
Loading