K Lee
K Lee

Reputation: 493

How to delete double quotation mark from ruby array?

Based on the link I tried to delete "" in the array on ruby

However still not get what I want, if anyone knows, please advice me

a = gets
lines = []
aaa = []
b = []
bb =[]
while line = gets do
  lines << line.chomp.split(' ')
end


for k in 0..(lines.size - 1) do
    b << lines[k][1].to_i + 1
end

for i in 0..(lines.size - 1)do
    bb << lines[i][0] + ' ' + b[i].to_s
end

for l in 0..(lines.size - 1)do
    p bb[l]
end

Input

3
Tanaka 18
Sato 50
Suzuki 120

Output

[["Tanaka", "18"], ["Sato", "50"], ["Suzuki", "120"]]
"Tanaka 19"
"Tanaka 19"
"Sato 51"
"Suzuki 121"

Upvotes: 0

Views: 160

Answers (1)

pjs
pjs

Reputation: 19855

As pointed out in the comments, you can get rid of the quotation marks by replacing p (Ruby's inspect/print) with puts.

While we're at it, you can make this much more "Ruby-ish" by using .readlines to scoop up all the input into an array, and by replacing the multiple counting loops with .map or .each iterators. The following is more concise, and allows you to lose the first input line which you're just throwing away anyway.

lines = STDIN.readlines(chomp: true).map do |line|
    l = line.split(' ')
    [l[0], l[1].to_i + 1].join(' ')
    # or
    # "#{l[0]} #{l[1].to_i + 1}"
end

lines.each { |line| puts line }

With Ruby 3, you can use rightward-assignment for the first part if you find it more readable:

STDIN.readlines(chomp: true).map do |line|
    l = line.split(' ')
    "#{l[0]} #{l[1].to_i + 1}"
end => lines

Upvotes: 1

Related Questions