# PrettyPrint Array?

**URL:** <https://www.ruby-forum.com/t/prettyprint-array/51998>\
**Category:** Ruby\
**Created:** [December 4, 2005, 2:50pm UTC](https://www.ruby-forum.com/t/prettyprint-array/51998 "2005-12-04T14:50:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [December 4, 2005, 2:50pm UTC](https://www.ruby-forum.com/t/prettyprint-array/51998/1 "2005-12-04T14:50:28Z")

</div>

I’m trying to make my own simple assert, but have problem displaying  
arrays.  
assert “123”, [1,2,[3]].to\_s

Is there a method or function x, fullfilling these requirements  
assert “[1,2,[3]]”, [1,2,[3]].x  
assert “[1,2,[3]]”, x([1,2,[3]])  
assert “1”, x(1)  
assert “a”, x(“a”)  
or some of them?

There is no problem with the comparison as  
assert [1,2,[3]], [1,2,[3]]

The problem appears when displaying different values:  
assert [1,2,[3]], [1,2,3]

…  
expect: 123  
actual: 123  
…

which is not very helpful.

This is the definition of my assert:

def assert(expect, actual)  
expect==actual ? print(".") : print("\nexpect: #{expect}\nactual:  
#{actual}\n")  
end

This is probably very easy, but I haven’t found anything.

Christer

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [December 4, 2005, 3:13pm UTC](https://www.ruby-forum.com/t/prettyprint-array/51998/2 "2005-12-04T15:13:04Z")

</div>

Hey Christer,

try [1,2,[3]].flatten.to\_s

it solves this one:  
assert [1,2,3], [1,2,[3]].flatten  
and probably is the easiest solution 🙂

however - assert not only compares and checks strings - you can use it  
for all  
other Objects as well… so “123”, [1,2,[3]].to\_s looks to me not that  
obvious - but i don’t know your approach and what this array should do -  
sending out this advice and waiting for the pros to comment your post ^^

MfG  
manveru

Am Sonntag, 4. Dezember 2005 14:50 schrieb Christer N.:

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [December 4, 2005, 6:29pm UTC](https://www.ruby-forum.com/t/prettyprint-array/51998/3 "2005-12-04T18:29:59Z")

</div>

On Sun, 04 Dec 2005 13:50:34 -0000, Christer N.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> actual: 123  
> …
> 
> which is not very helpful.

I’m not sure if this is what you’re after, but if it’s just about the  
displayed values try:

```
def assert(expect, actual)
  print (expect == actual ? "." : "\nexpect #{expect.inspect}\nactual:

```

#{actual.inspect}\n")  
end

which yields:

```
irb(main):029:0> assert([1,[2,3]], [1,2,3])

expect [1, [2, 3]]
actual: [1, 2, 3]
=> nil

```

Is that what you’re after?

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [December 5, 2005, 12:04am UTC](https://www.ruby-forum.com/t/prettyprint-array/51998/4 "2005-12-05T00:04:13Z")

</div>

Fellinger wrote:

> try [1,2,[3]].flatten.to\_s

No, I do not want to hide the difference.

rosco wrote:

> def assert(expect, actual)  
> print (expect == actual ? “.” : “\nexpect #{expect.inspect}\nactual:  
> #{actual.inspect}\n”)  
> end

Yes, “inspect” is exactly what I needed! Thank you very much!

Christer
