「Ruby入門」戻り値のメソッドを利用するサンプル

2020年12月29日

書式
return 値
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
#戻り値メソッドA
def testA(nn)
return nn + 2
end
#戻り値メソッドB
def testB(mm)
return mm - 15
end
#メソッドを呼び出す
value = testA(20)
value2 = testB(20)
#戻り値の利用
puts "testA method is called #{value}"
puts "testB method is called #{value2}"
#!/usr/bin/ruby # -*- coding: UTF-8 -*- #戻り値メソッドA def testA(nn) return nn + 2 end #戻り値メソッドB def testB(mm) return mm - 15 end #メソッドを呼び出す value = testA(20) value2 = testB(20) #戻り値の利用 puts "testA method is called #{value}" puts "testB method is called #{value2}"
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

#戻り値メソッドA
def testA(nn)
 return nn + 2
end

#戻り値メソッドB
def testB(mm)
 return mm - 15
end

#メソッドを呼び出す
value = testA(20)
value2 = testB(20)

#戻り値の利用
puts "testA method is called #{value}"
puts "testB method is called #{value2}"

実行結果
>ruby test.rb
testA method is called 22
testB method is called 5

Ruby

Posted by arkgame