# P shows object values, so can u put P and value in txt?

**URL:** <https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333>\
**Category:** Ruby\
**Created:** [December 10, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333 "2008-12-10T18:12:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 10, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333/1 "2008-12-10T18:12:05Z")

</div>

This is something that I have been working on using diff/lcs. I am  
beginner.

With my code (at bottom) I am now outputing to output window values like

#\<Diff::LCS::ContextChange:24252450 @action== positions=0,0  
elements="\n","\n"\>…

I need the ability to write to file and include above data such as  
@action== positions=0,0…etc. My code issue is that when I try to  
transfer this to a text file…it only writes the following.

#Diff::LCS::ContextChange:0x2e4215c  
#Diff::LCS::ContextChange:0x2e42044  
#Diff::LCS::ContextChange:0x2e41f2c  
#Diff::LCS::ContextChange:0x2e41dec  
#Diff::LCS::ContextChange:0x2e41d24  
#Diff::LCS::ContextChange:0x2e41c0c  
#Diff::LCS::ContextChange:0x2e41af4

Here’s code:  
require ‘rubygems’  
require ‘diff/lcs/array’

require ‘rubygems’  
require ‘diff/lcs/array’

lines1 = lines2 = nil  
File.open(“xml1.txt”) { |f| lines1 = f.readlines}  
File.open(“xml2.txt”) { |f| lines2 = f.readlines }

diffs = Diff::LCS.diff(lines1, lines2)  
sdiff = Diff::LCS.sdiff(lines1,lines2)

p sdiff = Diff::LCS.sdiff(lines1, lines2)

File.open(‘log.txt’, ‘w’) do |f1|  
f1.puts sdiff  
f1.close  
end

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 10, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333/2 "2008-12-10T21:50:43Z")

</div>

On 10.12.2008 18:05, Mmcolli00 Mom wrote:

> With my code (at bottom) I am now outputing to output window values like
> 
> #\<Diff::LCS::ContextChange:24252450 @action== positions=0,0  
> elements="\n","\n"\>…
> 
> I need the ability to write to file and include above data such as  
> @action== positions=0,0…etc. My code issue is that when I try to  
> transfer this to a text file…it only writes the following.

Try “ri p”.

robert

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 10, 2008, 10:33pm UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333/3 "2008-12-10T22:33:00Z")

</div>

I tried putting this but it did not work.

File.open(‘log.txt’, ‘a’) do |f1|  
f1.puts ri p lines2.diff(lines1)  
f1.close  
end

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 10, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333/4 "2008-12-10T22:43:03Z")

</div>

By ‘ri p’, it was meant that you do that on the command-line.  
(Actually, you’d want to type: ‘ri Kernel#p’)

‘ri’ provides documentation for Ruby classes/methods.

If you run the command ‘ri Kernel#p’, you’ll see that ‘p’ returns nil.  
So you can’t use it directly to write to a file. Have you tried ‘to\_s’?

f1.puts lines2.diff(lines1).to\_s

Offhand, I don’t know whether that works or not (don’t have diff  
installed at the moment).

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 11, 2008, 3:30am UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333/5 "2008-12-11T03:30:56Z")

</div>

On Wed, Dec 10, 2008 at 4:35 PM, Matthew M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> By ‘ri p’, it was meant that you do that on the command-line. (Actually,  
> you’d want to type: ‘ri Kernel#p’)
> 
> ‘ri’ provides documentation for Ruby classes/methods.
> 
> If you run the command ‘ri Kernel#p’, you’ll see that ‘p’ returns nil. So  
> you can’t use it directly to write to a file. Have you tried ‘to\_s’?

If the OP wants string output from what he sees with Kernel#p,  
Object#inspect should be used, not to\_s

> > [1,2,3].inspect  
> > =\> “[1, 2, 3]”  
> > [1,2,3].to\_s  
> > =\> “123”

This returns the same string that p will output to the console.

-greg

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 11, 2008, 4:36am UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333/6 "2008-12-11T04:36:34Z")

</div>

On Dec 10, 2008, at 8:23 PM, Gregory B. wrote:

> If the OP wants string output from what he sees with Kernel#p,  
> Object#inspect should be used, not to\_s

Yes, that. 😃

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 11, 2008, 4:07am UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333/7 "2008-12-11T04:07:02Z")

</div>

Greg

I spent all day on this. You have really helped me! Thanks so very  
much!!!

MC

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 11, 2008, 4:59am UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333/8 "2008-12-11T04:59:52Z")

</div>

I’d like to randomly interject the under-appreciated technique of  
printing an expression and its value, as opposed just the value.

def show(&block)  
expression = block.call.strip  
result = eval(expression, block.binding)  
printf("%-20s #=\> %s\n", expression, result.inspect)  
end

An example using the Array#slice docs,

a = [“a”, “b”, “c”, “d”, “e”]  
show {%{ a[2] + a[0] + a[1] }}  
show {%{ a[6] }}  
show {%{ a[1, 2] }}  
show {%{ a[1…3] }}  
show {%{ a[4…7] }}  
show {%{ a[6…10] }}  
show {%{ a[-3, 3] }}

verbatim output:  
a[2] + a[0] + a[1] #=\> “cab”  
a[6] #=\> nil  
a[1, 2] #=\> [“b”, “c”]  
a[1…3] #=\> [“b”, “c”, “d”]  
a[4…7] #=\> [“e”]  
a[6…10] #=\> nil  
a[-3, 3] #=\> [“c”, “d”, “e”]

(Binding#of\_caller could be used to circumvent the need for the block,  
but that has problems of its own.)

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 11, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/p-shows-object-values-so-can-u-put-p-and-value-in-txt/155333/9 "2008-12-11T15:57:00Z")

</div>

On Wed, Dec 10, 2008 at 10:53 PM, James M. Lawrence  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d like to randomly interject the under-appreciated technique of  
> printing an expression and its value, as opposed just the value.
> 
> def show(&block)  
> expression = block.call.strip  
> result = eval(expression, block.binding)  
> printf(“%-20s #=\> %s\n”, expression, result.inspect)  
> end

Neat idea. Thanks for sharing.

-greg
