「Ruby」メソッドを使うサンプル
書式
def 関数名
処理コード
end
使用例
#!/usr/bin/ruby
#クラスSampleの定義
class Sample
#初期化
def initialize
@mg = "STUDY SKILL"
end
#文字列を出力
def testFunc
p "元の文字列"
p @mg
p "文字列の長さ"
p @mg.length()
p "文字列の大文字"
p @mg.downcase()
end
end
#インスタンスを作成
cft = Sample.new
# testFuncを呼び出す
cft.testFunc
実行結果
“元の文字列"
“STUDY SKILL"
“文字列の長さ"
11
“文字列の大文字"
“study skill"