「Ruby」<<演算子で配列の要素を追加するサンプル

2021年2月4日

書式
配列名 +=「値」
配列名 <<「値」
使用例

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

cftA=["TA","TB","TC"]
 
cftA += ["TDD"]
puts "+演算子 result AA\n"  
p cftA
 
cftA += ["SE","SF","SH"]
puts "\n +演算子 result BB\n" 
p cftA

cftArr=["A001","B002","C003"]
 
cftArr << "D004"

puts "\n<<演算子 result CC\n" 
p cftArr

puts "\n << 演算子result DD\n" 
cftArr << "XX" << "YY" 
p cftArr

実行結果
>ruby sample.rb
+演算子 result AA
[“TA", “TB", “TC", “TDD"]

+演算子 result BB
[“TA", “TB", “TC", “TDD", “SE", “SF", “SH"]

<<演算子 result CC
[“A001", “B002", “C003", “D004"]

<< 演算子result DD
[“A001", “B002", “C003", “D004", “XX", “YY"]

Ruby

Posted by arkgame