abstract class Object
Overview
Object is the base type of all Crystal objects.
Included Modules
Direct Known Subclasses
Defined in:
primitives.crobject.cr
json/any.cr
json/to_json.cr
yaml/any.cr
yaml/to_yaml.cr
spec/expectations.cr
colorize.cr
Class Method Summary
- .==(other : Class)
- .===(other)
-
.cast(other) : self
Casts
other
to this class. - .from_json(string_or_io) : self
- .from_yaml(string : String) : self
- .hash
- .inspect(io)
-
.name : String
Returns the name of this class.
- .to_s(io)
-
.|(other : U.class)
Returns the union type of
self
andother
.
Instance Method Summary
-
#!=(other)
Returns true if this object is not equal to
other
. -
#!~(other)
Shortcut to
!(self =~ other)
-
#==(other)
Returns true if this object is equal to
other
. -
#===(other)
Case equality.
- #===(other : YAML::Any)
- #===(other : JSON::Any)
-
#=~(other)
Pattern match.
-
#class
Returns the runtime
Class
of an object. -
#clone
Returns a deep copy of this object.
-
#crystal_type_id
:nodoc
-
#dup
Returns a shallow copy of this object.
-
#hash
Generates an
Int
hash value for this object. -
#inspect
Returns a
String
representation of this object. -
#inspect(io : IO)
Appends a string representation of this object to the given IO object.
-
#itself
Return self.
-
#not_nil!
Returns self.
-
#tap(&block)
Yields self to the block, and then returns self.
- #to_json
- #to_pretty_json(io : IO)
- #to_pretty_json
-
#to_s
Returns a string representation of this object.
-
#to_s(io : IO)
Appends a String representation of this object to the given IO object.
- #to_yaml(io : IO)
- #to_yaml
-
#try(&block)
Yields self.
Macro Summary
-
def_equals(*fields)
Defines an
#==
method by comparing the given fields. - def_equals_and_hash(*fields)
-
def_hash(*fields)
Defines a
#hash
method computed from the given fields. -
delegate(method, required, *others)
Delegate method to to_object.
-
forward_missing_to(delegate)
Forwards missing methods to delegate.
-
getter(*names)
Defines getter methods for each of the given arguments.
-
getter!(*names)
Defines raise-on-nil and nilable getter methods for each of the given arguments.
-
getter?(*names)
Defines query getter methods for each of the given arguments.
-
property(*names)
Defines property methods for each of the given arguments.
-
property!(*names)
Defines raise-on-nil property methods for each of the given arguments.
-
property?(*names)
Defines query property methods for each of the given arguments.
-
setter(*names)
Defines setter methods for each of the given arguments.
Instance methods inherited from module Colorize::ObjectExtensions
colorizecolorize(fore) colorize
Instance methods inherited from module Spec::ObjectExtensions
should(expectation, file = __FILE__, line = __LINE__)
should,
should_not(expectation, file = __FILE__, line = __LINE__)
should_not
Class Method Detail
Casts other
to this class.
This is the same as using as
, but allows the class to be passed around as
an argument. See the [documentation on
as](//crystal-lang.org/docs/syntax_and_semantics/as.html) for more
information.
klass = Int32
number = [99, "str"][0]
typeof(number) # => (String | Int32)
typeof(klass.cast(number)) # => Int32
Returns the union type of self
and other
.
Int32 | Char # => (Int32 | Char)
Instance Method Detail
Returns true if this object is not equal to other
.
By default this method is implemented as !(self == other)
so there's no need to override this unless there's a more efficient
way to do it.
Returns true if this object is equal to other
.
Subclasses override this method to provide class-specific meaning.
Case equality.
The #===
method is used in a case ... when ... end
expression.
For example, this code:
case value
when x
# something when x
when y
# something when y
end
Is equivalent to this code:
if x === value
# something when x
elsif y === value
# something when y
end
Object simply implements #===
by invoking #==
, but subclasses
(notably Regex) can override it to provide meaningful case-equality semantics.
Pattern match.
Overridden by descendants (notably Regex and String) to provide meaningful pattern-match semantics.
Returns the runtime Class
of an object.
1.class # => Int32
"hello".class # => String
Compare it with typeof
, which returns the compile-time type of an object:
random_value = rand # => 0.627423
value = random_value < 0.5 ? 1 : "hello"
value # => "hello"
value.class # => String
typeof(value) # => Int32 | String
Returns a deep copy of this object.
Object returns self, but subclasses override this method to provide specific clone behaviour.
Returns a shallow copy of this object.
Object returns self, but subclasses override this method to provide specific dup behaviour.
Generates an Int
hash value for this object.
This method must have the property that a == b
implies a.hash == b.hash
.
The hash value is used along with #==
by the Hash
class to determine if two objects
reference the same hash key.
Returns a String
representation of this object.
Similar to #to_s
, but usually returns more information about
this object.
Classes must usually not override this method. Instead,
they must override #inspect(io)
, which must append to the
given IO object.
Appends a string representation of this object to the given IO object.
Similar to #to_s(io)
, but usually appends more information
about this object.
Yields self to the block, and then returns self.
The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
(1..10).tap { |x| puts "original: #{x.inspect}" }
.to_a.tap { |x| puts "array: #{x.inspect}" }
.select { |x| x % 2 == 0 }.tap { |x| puts "evens: #{x.inspect}" }
.map { |x| x*x }.tap { |x| puts "squares: #{x.inspect}" }
Returns a string representation of this object.
Descendants must usually not override this method. Instead,
they must override #to_s(io)
, which must append to the given
IO object.
Appends a String representation of this object to the given IO object.
An object must never append itself to the io argument,
as this will in turn call #to_s(io)
on it.
Yields self. Nil overrides this method and doesn't yield.
This method is useful for dealing with nilable types, to safely perform operations only when the value is not nil.
# First program argument in downcase, or nil
ARGV[0]?.try &.downcase
Macro Detail
Defines an #==
method by comparing the given fields.
The generated #==
method has a self restriction.
class Person
def initialize(@name, @age)
end
# Define a `==` method that compares @name and @age
def_equals @name, @age
end
Defines #hash
and #==
method from the given fields.
The generated #==
method has a self restriction.
class Person
def initialize(@name, @age)
end
# Define a hash method based on @name and @age
# Define a `==` method that compares @name and @age
def_equals_and_hash @name, @age
end
Defines a #hash
method computed from the given fields.
class Person
def initialize(@name, @age)
end
# Define a hash method based on @name and @age
def_hash @name, @age
end
Delegate method to to_object.
Syntax is: delegate method1, [method2, ..., ], to_object
Note that due to current language limitations this is only useful when no blocks are involved.
class StringWrapper
def initialize(@string)
end
delegate downcase, @string
delegate gsub, @string
delegate empty?, capitalize, @string
end
wrapper = StringWrapper.new "HELLO"
wrapper.downcase # => "hello"
wrapper.gsub(/E/, "A") # => "HALLO"
wrapper.empty? # => false
wrapper.capitalize # => "Hello"
Forwards missing methods to delegate.
class StringWrapper
def initialize(@string)
end
forward_missing_to @string
end
wrapper = StringWrapper.new "HELLO"
wrapper.downcase # => "hello"
wrapper.gsub(/E/, "A") # => "HALLO"
Defines getter methods for each of the given arguments.
Writing:
class Person
getter name
end
Is the same as writing:
class Person
def name
@name
end
end
The arguments can be string literals, symbol literals or plain names:
class Person
getter :name, "age"
end
If a type declaration is given, an instance variable with that name is declared with that type.
class Person
getter name : String
end
Is the same as writing:
class Person
@name : String
def name
@name
end
end
Defines raise-on-nil and nilable getter methods for each of the given arguments.
Writing:
class Person
getter! name
end
Is the same as writing:
class Person
def name?
@name
end
def name
@name.not_nil!
end
end
The arguments can be string literals, symbol literals or plain names:
class Person
getter! :name, "age"
end
If a type declaration is given, an instance variable with that name is declared with that type, as nilable.
class Person
getter! name : String
end
is the same as writing:
class Person
@name : String?
def name?
@name
end
def name
@name.not_nil!
end
end
Defines query getter methods for each of the given arguments.
Writing:
class Person
getter? happy
end
Is the same as writing:
class Person
def happy?
@happy
end
end
The arguments can be string literals, symbol literals or plain names:
class Person
getter? :happy, "famous"
end
If a type declaration is given, an instance variable with that name is declared with that type.
class Person
getter? happy : Bool
end
is the same as writing:
class Person
@happy : Bool
def happy?
@happy
end
end
Defines property methods for each of the given arguments.
Writing:
class Person
property name
end
Is the same as writing:
class Person
def name=(@name)
end
def name
@name
end
end
The arguments can be string literals, symbol literals or plain names:
class Person
property :name, "age"
end
If a type declaration is given, an instance variable with that name is declared with that type.
class Person
property name : String
end
Is the same as writing:
class Person
@name : String
def name=(@name)
end
def name
@name
end
end
Defines raise-on-nil property methods for each of the given arguments.
Writing:
class Person
property! name
end
Is the same as writing:
class Person
def name=(@name)
end
def name?
@name
end
def name
@name.not_nil!
end
end
The arguments can be string literals, symbol literals or plain names:
class Person
property! :name, "age"
end
If a type declaration is given, an instance variable with that name is declared with that type, as nilable.
class Person
property! name : String
end
Is the same as writing:
class Person
@name : String?
def name=(@name)
end
def name?
@name
end
def name
@name.not_nil!
end
end
Defines query property methods for each of the given arguments.
Writing:
class Person
property? happy
end
Is the same as writing:
class Person
def happy=(@happy)
end
def happy?
@happy
end
end
The arguments can be string literals, symbol literals or plain names:
class Person
property? :happy, "famous"
end
If a type declaration is given, an instance variable with that name is declared with that type.
class Person
property? happy : Bool
end
Is the same as writing:
class Person
@happy : Bool
def happy=(@happy)
end
def happy?
@happy
end
def happy
@happy.not_nil!
end
end
Defines setter methods for each of the given arguments.
Writing:
class Person
setter name
end
Is the same as writing:
class Person
def name=(@name)
end
end
The arguments can be string literals, symbol literals or plain names:
class Person
setter :name, "age"
end
If a type declaration is given, an instance variable with that name is declared with that type.
class Person
setter name : String
end
is the same as writing:
class Person
@name : String
def name=(@name)
end
end