I'm trying to get rid of the brackets [] and the new line \n from being printed.
My code looks like:
name1 = File.readlines('first.txt').sample(1)
name2 = File.readlines('middle.txt').sample(1)
name3 = File.readlines('last.txt').sample(1)
name = print (name1.strip
print name2.strip
print name3.strip)
puts name
I would like the output to look like JoshBobbyGreen.
However, it looks like:
[\"Josh\\n\"][\"Bobby\\n\"][\"Green\\n\"]
I've tried using .gsub, chomp and split but maybe I'm using them wrong.
Your code has a minor issue that causes the results you are experiencing.
when you use:
The returned value ISN'T a String, but rather an Array with 1 random sample. i.e:
This is why you get the output
["Jhon"]when usingprint.Since you expect (and prefer) a string, try this instead:
or:
or, probably what you meant, with no arguments,
samplewill return an object instead of an Array:Also, while printing, it would be better if you created one string to include all the spaces and formatting you wanted. i.e.: