# Array.each puzzle from new ruby-er

**URL:** <https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474>\
**Category:** Ruby\
**Created:** [August 7, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474 "2007-08-07T18:57:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c6cbf5/32.png) [@gani](https://www.ruby-forum.com/u/gani)\
**Post date:** [August 7, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474/1 "2007-08-07T18:57:57Z")

</div>

Given:  
lines = file.readlines("\x15") # don’t ask 🙂  
lines.each do |y|  
y.chomp!("\x15")  
y.sub!(/^\n/,"") #kill off the extra line feeds  
if y =~ /^\*\*\*/ then  
y = “” + y + “”  
…  
[assume ends]  
puts lines

my output for the affected elements is:  
  
value of y

How can I get the results to be  
value of y

I’ve tried changing y to y.to\_s; I still get the new line.

I’m sure this is obvious if you know what you’re doing, but I’m stumped.  
Can somebody give me a clue?

TIA if you take a moment to point me in the right direction.  
Gani

---

<div class="post-metadata">

**Author:** ![gani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c6cbf5/32.png) [@gani](https://www.ruby-forum.com/u/gani)\
**Post date:** [August 7, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474/2 "2007-08-07T19:08:32Z")

</div>

On 8/7/07, Gani R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts lines  
> I’m sure this is obvious if you know what you’re doing, but I’m stumped.  
> Can somebody give me a clue?
> 
> ## TIA if you take a moment to point me in the right direction. Gani
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hi Gani,

try  
y.gsub!(/^\n/,“”) #kill off the extra line feeds  
instead of  
y.sub!(/^\n/,“”) #kill off the extra line feeds

Ron

---

<div class="post-metadata">

**Author:** ![gani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c6cbf5/32.png) [@gani](https://www.ruby-forum.com/u/gani)\
**Post date:** [August 7, 2007, 7:13pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474/3 "2007-08-07T19:13:32Z")

</div>

Gani R. wrote:

> Given:  
> lines = file.readlines("\x15") # don’t ask 🙂  
> lines.each do |y|  
> y.chomp!("\x15")  
> y.sub!(/^\n/,"") #kill off the extra line feeds  
> I would think it better to just call y.chomp! here (with no argument).

> ```
> if y =~ /^\*\*\*/ then
> y = "<someTag>" + y + "</someTag>"
> 
> ```

Know that this is actually rebinding the local variable y to point to a  
new object, not modifying the old object. If your goal was to modify the  
array “lines”, you should change “lines.each” to “lines.map” and make  
sure you end the block with a statement that returns the modified y.

perhaps:  
lines.map do |y|  
y = y.chomp("\x15").chomp  
y = “” + y + “” if y =~ /^\*\*\*/  
y  
end

Dan

---

<div class="post-metadata">

**Author:** ![gani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c6cbf5/32.png) [@gani](https://www.ruby-forum.com/u/gani)\
**Post date:** [August 7, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474/4 "2007-08-07T19:15:32Z")

</div>

On 8/7/07, ronald braswell [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > if y =~ /^\*\*\*/ then
> > 
> > ```
> > 
> > value of y  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Oops. That would not be any different because of the ^anchor.  
y.sub!(/^\n+/,“”) might do the trick.

Ron

---

<div class="post-metadata">

**Author:** ![gani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c6cbf5/32.png) [@gani](https://www.ruby-forum.com/u/gani)\
**Post date:** [August 7, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474/5 "2007-08-07T20:04:43Z")

</div>

> Know that this is actually rebinding the local variable y to point to a  
> new object, not modifying the old object. If your goal was to modify the  
> array “lines”, you should change “lines.each” to “lines.map” and make  
> sure you end the block with a statement that returns the modified y.
> 
> perhaps:  
> lines.map do |y|  
> y = y.chomp("\x15").chomp  
> y = “” + y + “” if y =~ /^\*\*\*/  
> y  
> end
> 
> Dan

Sorry, actually doing each\_with\_index do |y,ind| (etc) and specifically  
setting lines[ind] = y  
but my original question remains: how do i make the “” + y leave  
off the \n so the output is  
text of y  
Thanks again

---

<div class="post-metadata">

**Author:** ![gani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c6cbf5/32.png) [@gani](https://www.ruby-forum.com/u/gani)\
**Post date:** [August 7, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474/6 "2007-08-07T20:53:47Z")

</div>

Gani R. wrote:

> > end
> > 
> > Dan
> 
> Sorry, actually doing each\_with\_index do |y,ind| (etc) and specifically  
> setting lines[ind] = y  
> but my original question remains: how do i make the “” + y leave  
> off the \n so the output is  
> text of y  
> Thanks again

Sorry, I thought that would work but didn’t test it. “y.lstrip” will do  
what you want, but will also will strip leading whitespace, which might  
not be okay. If not, “y[1…-1]” should suffice–this will return y from  
the second character on.  
y = y[1…-1] if y =~ /^\n/

Dan

---

<div class="post-metadata">

**Author:** ![gani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c6cbf5/32.png) [@gani](https://www.ruby-forum.com/u/gani)\
**Post date:** [August 7, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474/7 "2007-08-07T20:41:45Z")

</div>

On Aug 7, 2:04 pm, Gani R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > Posted viahttp://www.ruby-forum.com/.  
> > Have you considered switching to map?

As for debugging, I’d try tracking it down using p

lines = file.readlines(“\x15”).map do |line|  
line.chomp!(“\x15”)  
line.sub!(/^\n/, “”)  
line = “” + line + “” if line =~ /^\*{3}/  
p [:line, line] # comment me out after debugging  
line  
end

p [:lines, lines] # comment me out after debugging

puts lines

---

<div class="post-metadata">

**Author:** ![gani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c6cbf5/32.png) [@gani](https://www.ruby-forum.com/u/gani)\
**Post date:** [August 7, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474/8 "2007-08-07T21:10:58Z")

</div>

Noah E. wrote:

> On Aug 7, 2:04 pm, Gani R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > end  
> > > Posted viahttp://www.ruby-forum.com/.  
> > > Have you considered switching to map?
> 
> As for debugging, I’d try tracking it down using p
> 
> lines = file.readlines(“\x15”).map do |line|  
> line.chomp!(“\x15”)  
> line.sub!(/^\n/, “”)  
> line = “” + line + “” if line =~ /^\*{3}/  
> p [:line, line] # comment me out after debugging  
> line  
> end
> 
> p [:lines, lines] # comment me out after debugging
> 
> puts lines

Oh. I did a subset of the p [:line, line] and it looks like i had some  
\n and some \r\n occurring. I was just pulling the \r\n on my sub call  
(wrote it as \n to simplify)because when I was looking at the original  
with end of line showing they all showed as CRLF. Odd.  
Much improved output, and another tool in my meager toolkit. Thanks so  
much, Noah and all other repliers! I’ll look at replacing the each with  
map/collect now that I know about it. I do have the Pragmatic  
programming book…  
Cheers, Gani

---

<div class="post-metadata">

**Author:** ![gani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c6cbf5/32.png) [@gani](https://www.ruby-forum.com/u/gani)\
**Post date:** [August 7, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/array-each-puzzle-from-new-ruby-er/111474/9 "2007-08-07T22:15:59Z")

</div>

On Aug 7, 9:10 pm, Gani R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > line.sub!(/^\n/, “”)  
> > \n and some \r\n occurring. I was just pulling the \r\n on my sub call  
> > (wrote it as \n to simplify)because when I was looking at the original  
> > with end of line showing they all showed as CRLF. Odd.  
> > Much improved output, and another tool in my meager toolkit. Thanks so  
> > much, Noah and all other repliers! I’ll look at replacing the each with  
> > map/collect now that I know about it. I do have the Pragmatic  
> > programming book…  
> > Cheers, Gani
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Piece of advice - concatenation in Ruby is very fast but you should  
take the better way by using:  
y = “” \<\< y \<\< “”  
which is a little faster with same readability 😉
