削除された内容 追加された内容
Ef3 (トーク | 投稿記録)
→‎Tupleオブジェクト: Tupleオブジェクトは、任意の Crystal オブジェクトを要素として持つことができます。 配列式<code>{ 要素1, 要素2, … 要素n }</code> で生成します。
タグ: 2017年版ソースエディター
Ef3 (トーク | 投稿記録)
→‎オブジェクト: Crystal では、全てがオブジェクトです。
タグ: 2017年版ソースエディター
1,290 行
: Integer#times にブロックを渡さないと、Int::TimesIterator([T])オブジェクトが返ります。
: Int::TimesIterator([T])オブジェクトは外部イテレーターと呼ばれnextメソッドで反復を行えます。
 
== オブジェクト ==
Crystal では、全てがオブジェクトです。
 
=== オブジェクトのリテラルとクラス ===
;オブジェクトのリテラルとクラス:<syntaxhighlight lang=crystal>
[nil, false, true, 1, 3.14, "abc", :abc, 1..10, 1...10, 1..,
[1, 2_u8, 3_i128],
[1, 2, 3], [1, "abc"],
{1, 2, 3}, {1, "abc"},
{"a" => 1, "b" => 2},
{a: 1, b: 2},
Set.new([:a, :bc, :def]),
->(x : Int32) { 2 * x },
100.times,
(1..).each,
[1, 2, 3].each,
{1, 2, 3}.each,
{"a" => 1, "b" => 2}.each,
# {a:1, b:2}.each, # Error: 'NamedTuple(a: Int32, b: Int32)#each' is expected to be invoked with a block, but no block was given
].each do |obj|
p [obj, obj.class]
end
</syntaxhighlight>
;実行結果:<syntaxhighlight lang="text" style="overflow: scroll;height:12em">
[nil, Nil]
[false, Bool]
[true, Bool]
[1, Int32]
[3.14, Float64]
["abc", String]
[:abc, Symbol]
[1..10, Range(Int32, Int32)]
[1...10, Range(Int32, Int32)]
[1.., Range(Int32, Nil)]
[[1, 2, 3], Array(Int128 | Int32 | UInt8)]
[[1, 2, 3], Array(Int32)]
[[1, "abc"], Array(Int32 | String)]
[{1, 2, 3}, Tuple(Int32, Int32, Int32)]
[{1, "abc"}, Tuple(Int32, String)]
[{"a" => 1, "b" => 2}, Hash(String, Int32)]
[{a: 1, b: 2}, NamedTuple(a: Int32, b: Int32)]
[Set{:a, :bc, :def}, Set(Symbol)]
[#<Proc(Int32, Int32):0x5a6c0ffabcf0>, Proc(Int32, Int32)]
[#<Int::TimesIterator(Int32):0x7e2b4be59e80 @n=100, @index=0>, Int::TimesIterator(Int32)]
[#<Range::ItemIterator(Int32, Nil):0x7e2b4be5dfc0 @range=1.., @current=1, @reached_end=false>, Range::ItemIterator(Int32, Nil)]
[#<Indexable::ItemIterator(Array(Int32), Int32):0x7e2b4be58da0 @array=[1, 2, 3], @index=0>, Indexable::ItemIterator(Array(Int32), Int32)]
[#<Indexable::ItemIterator(Tuple(Int32, Int32, Int32), Int32):0x7e2b4be5dfa0 @array={1, 2, 3}, @index=0>, Indexable::ItemIterator(Tuple(Int32, Int32, Int32), Int32)]
[#<Hash::EntryIterator(String, Int32):0x7e2b4be58d80 @hash={"a" => 1, "b" => 2}, @index=0>, Hash::EntryIterator(String, Int32)]
</syntaxhighlight>
 
=== Rubyのオブジェクトのリテラルとクラス ===
;Rubyのオブジェクトのリテラルとクラス:<syntaxhighlight lang=ruby>
require 'set'
 
[nil, false, true, 1, 3.14, "abc", :abc, 1..10, 1...10, 1..,
# [1, 2_u8, 3_i128],
[1, 2, 3], [1, "abc"],
# {1, 2, 3}, {1, "abc"},
{"a" => 1, "b" => 2},
{a: 1, b: 2},
Set.new([:a, :bc, :def]),
->(x) { 2 * x },
100.times,
(1..).each,
[1, 2, 3].each,
# {1, 2, 3}.each,
{"a" => 1, "b" => 2}.each,
# {a:1, b:2}.each, # Error: 'NamedTuple(a: Int32, b: Int32)#each' is expected to be invoked with a block, but no block was given
].each do |obj|
p [obj, obj.class]
end
</syntaxhighlight>
;実行結果:<syntaxhighlight lang="text" style="overflow: scroll;height:12em">
[nil, NilClass]
[false, FalseClass]
[true, TrueClass]
[1, Integer]
[3.14, Float]
["abc", String]
[:abc, Symbol]
[1..10, Range]
[1...10, Range]
[1.., Range]
[[1, 2, 3], Array]
[[1, "abc"], Array]
[{"a"=>1, "b"=>2}, Hash]
[{:a=>1, :b=>2}, Hash]
[#<Set: {:a, :bc, :def}>, Set]
[#<Proc:0x000014af26147eb0 Main.rb:10 (lambda)>, Proc]
[#<Enumerator: 100:times>, Enumerator]
[#<Enumerator: 1..:each>, Enumerator]
[#<Enumerator: [1, 2, 3]:each>, Enumerator]
[#<Enumerator: {"a"=>1, "b"=>2}:each>, Enumerator]
</syntaxhighlight>
 
== メソッド ==
オブジェクトの値や機能を呼び出すためには、メソッドを使います(多くの演算子もメソッドです)。
 
=== クラスのメソッド一覧 ===
Crystal には、Objectクラスにmethodsメソッドがないので、マクロで実装しました。
 
;RubyのObject#methods:<syntaxhighlight lang=rubycrystal>
p Object.methods.sort,
Integer.methods.sort,
1,302 ⟶ 1,399行目:
Range.methods.sort
</syntaxhighlight>
;実行結果:<syntaxhighlight lang="text" style="overflow: scroll;height:7em12em">
[:!, :!=, :!~, :<, :<=, :<=>, :==, :===, :=~, :>, :>=, :__id__, :__send__, :alias_method, :allocate, :ancestors, :attr, :attr_accessor, :attr_reader, :attr_writer, :autoload, :autoload?, :class, :class_eval, :class_exec, :class_variable_defined?, :class_variable_get, :class_variable_set, :class_variables, :clone, :const_defined?, :const_get, :const_missing, :const_set, :const_source_location, :constants, :define_method, :define_singleton_method, :deprecate_constant, :display, :dup, :enum_for, :eql?, :equal?, :extend, :freeze, :frozen?, :hash, :include, :include?, :included_modules, :inspect, :instance_eval, :instance_exec, :instance_method, :instance_methods, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :is_a?, :itself, :kind_of?, :method, :method_defined?, :methods, :module_eval, :module_exec, :name, :new, :nil?, :object_id, :prepend, :private_class_method, :private_constant, :private_instance_methods, :private_method_defined?, :private_methods, :protected_instance_methods, :protected_method_defined?, :protected_methods, :public_class_method, :public_constant, :public_instance_method, :public_instance_methods, :public_method, :public_method_defined?, :public_methods, :public_send, :remove_class_variable, :remove_instance_variable, :remove_method, :respond_to?, :send, :singleton_class, :singleton_class?, :singleton_method, :singleton_methods, :subclasses, :superclass, :taint, :tainted?, :tap, :then, :to_enum, :to_s, :trust, :undef_method, :untaint, :untrust, :untrusted?, :yield_self]
[:!, :!=, :!~, :<, :<=, :<=>, :==, :===, :=~, :>, :>=, :__id__, :__send__, :alias_method, :allocate, :ancestors, :attr, :attr_accessor, :attr_reader, :attr_writer, :autoload, :autoload?, :class, :class_eval, :class_exec, :class_variable_defined?, :class_variable_get, :class_variable_set, :class_variables, :clone, :const_defined?, :const_get, :const_missing, :const_set, :const_source_location, :constants, :define_method, :define_singleton_method, :deprecate_constant, :display, :dup, :enum_for, :eql?, :equal?, :extend, :freeze, :frozen?, :hash, :include, :include?, :included_modules, :inspect, :instance_eval, :instance_exec, :instance_method, :instance_methods, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :is_a?, :itself, :kind_of?, :method, :method_defined?, :methods, :module_eval, :module_exec, :name, :nil?, :object_id, :prepend, :private_class_method, :private_constant, :private_instance_methods, :private_method_defined?, :private_methods, :protected_instance_methods, :protected_method_defined?, :protected_methods, :public_class_method, :public_constant, :public_instance_method, :public_instance_methods, :public_method, :public_method_defined?, :public_methods, :public_send, :remove_class_variable, :remove_instance_variable, :remove_method, :respond_to?, :send, :singleton_class, :singleton_class?, :singleton_method, :singleton_methods, :sqrt, :subclasses, :superclass, :taint, :tainted?, :tap, :then, :to_enum, :to_s, :trust, :try_convert, :undef_method, :untaint, :untrust, :untrusted?, :yield_self]
1,345 ⟶ 1,442行目:
Proc.methods
</syntaxhighlight>
;実行結果:<syntaxhighlight lang="text" style="overflow: scroll;height:7em12em">
Object.methods # => ["!=", "!~", "==", "===", "=~", "class", "crystal_type_id", "dup", "hash", "in?", "inspect", "itself", "not_nil!", "pretty_inspect", "pretty_print", "tap", "to_s", "try", "unsafe_as"]
Reference.methods # => ["==", "dup", "exec_recursive", "exec_recursive_clone", "hash", "inspect", "object_id", "pretty_print", "same?", "to_s"]