「Crystal」の版間の差分
削除された内容 追加された内容
→マクロ p!: メソッド p は、与えられた「式」の inspaect() の返す値を puts しますが、マクロ p! は、それに先んじて(評価前の)「式」を表示します<ref>[https://crystal-lang.org/api/1.5.0/Crystal/Macros.html#p%21%28%2Aexpressions%29%3ANop-instance-method def p!(*expressions) : Nop]</ref>。 タグ: 2017年版ソースエディター |
タグ: 2017年版ソースエディター |
||
251 行
=== ユーザー定義クラス ===
==== シンプルなクラス ====
;シンプルなクラス:<syntaxhighlight lang=crystal line>
class Hello
def initialize(@name : String = "World")
end
def greeting
puts "Hello #{@name}!"
end
end
hello = Hello.new()
hello.greeting
universe = Hello.new("Universe")
universe.greeting
</syntaxhighlight>
;実行結果:<syntaxhighlight lang=text>
Hello World!
Hello Universe!
</syntaxhighlight>
==== 都市間の大圏距離 ====
[[Ruby#ユーザー定義クラス]]の都市間の大圏距離を求めるメソッドを追加した例を、Crystalに移植しました。
|