[Ruby]joinメソッドで配列内の値を加算を求める

2021年8月29日

書式
eval 配列名.join(“+")
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/usr/bin/ruby
#クラスSampleの定義
class Sample
#文字列を出力
def testFunc
cft=[4,5,6,7,8]
p cft.join("+")
p eval cft.join("+")
end
end
#インスタンスを作成
cft = Sample.new
# testFuncを呼び出す
cft.testFunc
#!/usr/bin/ruby #クラスSampleの定義 class Sample #文字列を出力 def testFunc cft=[4,5,6,7,8] p cft.join("+") p eval cft.join("+") end end #インスタンスを作成 cft = Sample.new # testFuncを呼び出す cft.testFunc
#!/usr/bin/ruby
#クラスSampleの定義
class Sample
 #文字列を出力
  def testFunc
  cft=[4,5,6,7,8]
  p cft.join("+")
  p eval cft.join("+")
  end
end
#インスタンスを作成
cft = Sample.new
# testFuncを呼び出す
cft.testFunc

実行結果
30

Ruby

Posted by arkgame