10

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.

spenibus
  • 4,339
  • 11
  • 26
  • 35
marriedjane875
  • 653
  • 2
  • 10
  • 24

6 Answers6

31

consider also optional chomp parameter in File.readlines

File.readlines("/path/to/file", chomp: true)
M. Modugno
  • 741
  • 6
  • 14
12

puts adds a newline at the end of the output. print does not. Use print. It may solve your issue. Also, use .strip.

"\tgoodbye\r\n".strip   #=> "goodbye"
Khoga
  • 857
  • 2
  • 8
  • 26
3

Your code has a minor issue that causes the results you are experiencing.

when you use:

name1 = File.readlines('first.txt').sample(1)

The returned value ISN'T a String, but rather an Array with 1 random sample. i.e:

["Jhon"]

This is why you get the output ["Jhon"] when using print.

Since you expect (and prefer) a string, try this instead:

name1 = File.readlines('first.txt').sample(1)[0]
name2 = File.readlines('middle.txt').sample(1)[0]
name3 = File.readlines('last.txt').sample(1)[0]

or:

name1 = File.readlines('first.txt').sample(1).pop
name2 = File.readlines('middle.txt').sample(1).pop
name3 = File.readlines('last.txt').sample(1).pop

or, probably what you meant, with no arguments, sample will return an object instead of an Array:

name1 = File.readlines('first.txt').sample
name2 = File.readlines('middle.txt').sample
name3 = File.readlines('last.txt').sample

Also, while printing, it would be better if you created one string to include all the spaces and formatting you wanted. i.e.:

name1 = File.readlines('first.txt').sample(1).pop
name2 = File.readlines('middle.txt').sample(1).pop
name3 = File.readlines('last.txt').sample(1).pop

puts "#{name1} #{name2} #{name3}."
# or
print "#{name1} #{name2} #{name3}."
Myst
  • 18,516
  • 2
  • 45
  • 67
1

If:

name1 = ["Josh\n"]
name2 = ["Bobby\n"]
name3 = ["Green\n"]

it's just:

puts [name1, name2, name3].map { |a| a.first.strip }.join('')
  #=> JoshBobbyGreen
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
  • I use `&:strip` exactly? because I get a `in map undefined method strip, for...` – marriedjane875 Sep 27 '15 at 02:54
  • Sorry, I don't follow. Do you get an exception running this? I'm sure the code's fine. Incidentally, `map(&:strip)` yields the same result as `map { |s| s.strip }`. – Cary Swoveland Sep 27 '15 at 03:01
  • I wasn't sure if `&:strip` was a typo – marriedjane875 Sep 27 '15 at 03:05
  • but it says "in map: undefined method 'strip'" – marriedjane875 Sep 27 '15 at 03:06
  • That's very odd. `name1`, `name2` and `name3` are strings and the Class string certainly has a method `strip`. The the code runs fine for me with Ruby v2.2 (and I don't think it could be a version issue). Did you use my definitions `name1`, `name2` and `name3`? If not, I'd need to know the values of those variables to diagnose the problem. – Cary Swoveland Sep 27 '15 at 05:14
  • the solution was written above, but thank you for your help :) I had no idea either, so I appreciate it. – marriedjane875 Sep 27 '15 at 05:17
  • It's because `Array#sample(n)` returns an array, there's no `strip` method in `Array` – sbs Jan 29 '16 at 06:47
  • @sbs, because of `map`, `String#strip` is invoked on each element of the array `[name1,name2,name3]`, which is a string. – Cary Swoveland Jan 29 '16 at 07:38
  • @CarySwoveland, in OP's original question, `name1` is an Array. – sbs Jan 29 '16 at 07:53
  • It's not working because you're calling `a.first.strip`. In this example, `a` reference each element of the array, which is a string. You're then calling `a.first`, which there is no method `first` for string. Take out the `.first` and it'll work as expected. – evanthegrayt Apr 16 '20 at 13:35
  • @evanthegrayt, `a` references each element of `[["Josh\n"], ["Bobby\n"], ["Green\n"]]`, the first being `a = ["Josh\n"]`. `a.first #=> "Josh\n"`, so `a.first.strip #=> "Josh"`. – Cary Swoveland Apr 16 '20 at 14:03
  • @Cary Ah, apologies, I totally missed that each element was an array itself. – evanthegrayt Apr 16 '20 at 17:43
0

So as to avoid redundant \n's I'd rather use split:

File.read('first.txt').split("\n") # double quotes are important!

So as to avoid getting array use sample method without arguments:

File.read('first.txt').split("\n").sample
Nick Roz
  • 3,918
  • 2
  • 36
  • 57
-1
puts %w(first middle last).map { |e| IO.readlines("#{e}.txt").sample.strip }.join
sbs
  • 4,102
  • 5
  • 40
  • 54