# Formatting issue

**URL:** <https://www.ruby-forum.com/t/formatting-issue/154508>\
**Category:** Rails\
**Created:** [December 1, 2008, 12:36am UTC](https://www.ruby-forum.com/t/formatting-issue/154508 "2008-12-01T00:36:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaughajj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/0ea827/32.png) [@vaughajj](https://www.ruby-forum.com/u/vaughajj)\
**Post date:** [December 1, 2008, 12:36am UTC](https://www.ruby-forum.com/t/formatting-issue/154508/1 "2008-12-01T00:36:33Z")

</div>

I have a text file that has data in it which is perfectly formatted the  
way I want it.  
I have the following code in my controller to open the file and read  
each line into an array which I then call from my view. The problem is,  
I completely loose the formatting the way I’m doing it:

- from my controller:  
`
def index
@ssh = Array.new
a = File.open("/usr/bin/my_data.dat")
a.each do |line|
@my_data.push(line)
end
end
`

-from my view:  
`
<%= @my_data %>
`

I’m thinking that maybe I shouldn’t put the data in an array or maybe I  
should somehow loop through the array in my view in order to maintain  
the formatting of the text file?

thanks

---

<div class="post-metadata">

**Author:** ![vaughajj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/0ea827/32.png) [@vaughajj](https://www.ruby-forum.com/u/vaughajj)\
**Post date:** [December 1, 2008, 1:07am UTC](https://www.ruby-forum.com/t/formatting-issue/154508/2 "2008-12-01T01:07:24Z")

</div>

On Nov 30, 11:36 pm, John V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have a text file that has data in it which is perfectly formatted the  
> way I want it.  
> I have the following code in my controller to open the file and read  
> each line into an array which I then call from my view. The problem is,  
> I completely loose the formatting the way I’m doing it:

\<%= … %\> basically just calls to\_s on whatever the expression in  
there evaluates to, and to\_s on an Array is just the concatenation of  
to\_s called on everything in it. If you just want to display data from  
the file, why not use @my\_data = File.read(…) ?

Fred

---

<div class="post-metadata">

**Author:** ![vaughajj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/0ea827/32.png) [@vaughajj](https://www.ruby-forum.com/u/vaughajj)\
**Post date:** [December 1, 2008, 5:27am UTC](https://www.ruby-forum.com/t/formatting-issue/154508/3 "2008-12-01T05:27:40Z")

</div>

On Sun, Nov 30, 2008 at 6:03 PM, John V.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here is the data, if you put it in a file and then tell Rails to read  
> it, please see if you can get it to display in the exact same format in  
> Rails. When I do it, all my columns don’t line up:

They don’t line up in this email, either 🙂

Are you trying to display this in PRE tags or equivalent CSS with a  
monospace font?

Your display would probably be more robust if you parsed the file  
contents and put it in an actual HTML table, though.

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![vaughajj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/0ea827/32.png) [@vaughajj](https://www.ruby-forum.com/u/vaughajj)\
**Post date:** [December 1, 2008, 3:03am UTC](https://www.ruby-forum.com/t/formatting-issue/154508/4 "2008-12-01T03:03:18Z")

</div>

Here is the data, if you put it in a file and then tell Rails to read  
it, please see if you can get it to display in the exact same format in  
Rails. When I do it, all my columns don’t line up:

Mod Ports Card Type Model  
Serial No.

* * *

* * *

1 48 48 port 10/100 mb RJ-45 ethernet WS-X6248-RJ-45  
SAD041509MC  
2 48 48 port 10/100 mb RJ-45 ethernet WS-X6248-RJ-45  
SAD041206F6  
3 16 16 port 1000mb MTRJ ethernet WS-X6416-GE-MT  
SAD04220ML4  
4 48 48 port 10/100 mb RJ-45 ethernet WS-X6248-RJ-45  
SAD03465383  
5 2 Supervisor Engine 720 (Active) WS-SUP720-3B  
SAL092750VP  
6 2 Supervisor Engine 720 (Warm) WS-SUP720-3B  
SAL09285BW6  
7 48 48 port 10/100 mb RJ-45 ethernet WS-X6248-RJ-45  
SAD041509MY  
8 48 CEF720 48 port 10/100/1000mb Ethernet WS-X6748-GE-TX  
SAL09285L96  
9 6 Firewall Module WS-SVC-FWM-1  
SAD084305Y3

Mod MAC addresses Hw Fw Sw  
Status

* * *

* * *

1 0001.970a.dab0 to 0001.970a.dadf 1.2 5.1(1)CSX 8.5(0.46)ROC  
Ok  
2 00b0.c2f1.f21c to 00b0.c2f1.f24b 1.2 5.1(1)CSX 8.5(0.46)ROC  
Ok  
3 00d0.c0ce.f854 to 00d0.c0ce.f863 1.0 5.3(1) 8.5(0.46)ROC  
Ok  
4 0030.9615.2414 to 0030.9615.2443 1.1 4.2(0.24)VAI 8.5(0.46)ROC  
Ok  
5 0012.dae4.7a28 to 0012.dae4.7a2b 4.4 8.1(3) 12.2(18)SXE3  
Ok  
6 0012.dae4.7f80 to 0012.dae4.7f83 4.4 8.1(3) 12.2(18)SXE3  
Ok  
7 0001.974a.0a30 to 0001.974a.0a5f 1.2 5.1(1)CSX 8.5(0.46)ROC  
Ok  
8 0014.f27e.b460 to 0014.f27e.b48f 2.2 12.2(14r)S5 12.2(18)SXE3  
Ok  
9 0003.3234.bb36 to 0003.3234.bb3d 3.0 7.2(1) 2.3(1)  
Ok

Mod Sub-Module Model Serial Hw  
Status

* * *

* * *

5 Policy Feature Card 3 WS-F6K-PFC3B SAL09253YMB 2.1  
Ok  
5 MSFC3 Daughterboard WS-SUP720 SAL0926479K 2.3  
Ok  
6 Policy Feature Card 3 WS-F6K-PFC3B SAL09274WMF 2.1  
Ok  
6 MSFC3 Daughterboard WS-SUP720 SAL09169JJE 2.3  
Ok  
8 Centralized Forwarding Card WS-F6700-CFC SAL09316RQD 2.0  
Ok

Mod Online Diag Status

* * *

1 Pass  
2 Pass  
3 Pass  
4 Pass  
5 Pass  
6 Pass  
7 Pass  
8 Pass  
9 Pass

thanks for the help,

vic

---

<div class="post-metadata">

**Author:** ![vaughajj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/0ea827/32.png) [@vaughajj](https://www.ruby-forum.com/u/vaughajj)\
**Post date:** [December 1, 2008, 1:14pm UTC](https://www.ruby-forum.com/t/formatting-issue/154508/5 "2008-12-01T13:14:55Z")

</div>

On 1 Dec 2008, at 11:44, John V. wrote:

> ## @my\_data = `/usr/bin/my_data.rb`#this causes data to be skewed #@my\_data = File.open("/usr/bin/my\_data.dat").read #this works end
> 
> index.rthml code:
> 
> ```
> <%= simple_format @my_data %>
> ```

puts always appends a carriage return whereas write doesn’t. if you  
changed it to STDOUT.write(line) it should be the same

Fred

---

<div class="post-metadata">

**Author:** ![vaughajj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/0ea827/32.png) [@vaughajj](https://www.ruby-forum.com/u/vaughajj)\
**Post date:** [December 1, 2008, 12:44pm UTC](https://www.ruby-forum.com/t/formatting-issue/154508/6 "2008-12-01T12:44:12Z")

</div>

Hassan S. wrote:

> They don’t line up in this email, either 🙂  
> …hehe…I know, I’m sorry that wasn’t much of a help 🙂

> Are you trying to display this in PRE tags or equivalent CSS with a  
> monospace font?

…good call on this one though, I added PRE tags on my index.rhtml and  
the data is looking the way I want it…which is great news.

## I am still a bit baffled as to why I have to write the data to a file first, then open the file and put it in PRE tags as opposed to using PUTS statements in my original script and assigning that to a variable which gets called by my index page which still causes the format to be skewed.

## my\_data.rb code: data.each do |line| a.write(line) #–this writes to my\_data.dat puts line #–this puts the same data to STDOUT end

## controller code: def index @my\_data = `/usr/bin/my_data.rb`#this causes data to be skewed #@my\_data = File.open("/usr/bin/my\_data.dat").read #this works end

index.rthml code:

```
<%= simple_format @my_data %>
```

Perhaps the way I’m writing to STDOUT in my\_data.rb is causing the  
skewing of my data?

thanks

vic

---

<div class="post-metadata">

**Author:** ![vaughajj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/0ea827/32.png) [@vaughajj](https://www.ruby-forum.com/u/vaughajj)\
**Post date:** [December 2, 2008, 12:01am UTC](https://www.ruby-forum.com/t/formatting-issue/154508/7 "2008-12-02T00:01:26Z")

</div>

STDOUT.write(line) fixed the problem…thanks again for the help!

Frederick C. wrote:

> On 1 Dec 2008, at 11:44, John V. wrote:
> 
> > ## @my\_data = `/usr/bin/my_data.rb`#this causes data to be skewed #@my\_data = File.open("/usr/bin/my\_data.dat").read #this works end
> > 
> > index.rthml code:
> > 
> > ```
> > <%= simple_format @my_data %>
> > ```
> 
> puts always appends a carriage return whereas write doesn’t. if you  
> changed it to STDOUT.write(line) it should be the same
> 
> Fred
