0

Hi amazing stackers!

date = "10/02/2021"
d1 = date.split("/")
d2 = d1.to_s
puts d1
puts d1.class
puts d2
puts d2.class

in the above code, d2 shows the data type "string" but it is displayed like an array. And d1 is an array but it doesn't have an array like form. Can you please resolve the confusion?

2 Answers 2

1

This is what you are expecting

date = "10/02/2021"
d1 = date.split("/")
d2 = d1.join
p "--------------------------"
p d1
p d1.class
p d2
p d2.class
p "--------------------------"

Output

"--------------------------"
["10", "02", "2021"]
Array
"10022021"
String
"--------------------------"

Use p instead puts, it will print the data along with it's structure. Use join to combine the array element into a string.

Sign up to request clarification or add additional context in comments.

Comments

0

Please see the documentation for the full details but

Array#to_s creates a string representation of the array by calling inspect on each element. Basically this creates a string that looks the way you would expect an array to look if you inspected it, ie p d2.

IO#puts when called with an array as the argument writes each element on a new line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.