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

M

Mmcolli00 Mom

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
 
R

Robert Klemme

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
 
M

Mmcolli00 Mom

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
 
M

Matthew Moss

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).
 
G

Gregory Brown

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
 
M

Mmcolli00 Mom

Greg

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

MC
 
J

James M. Lawrence

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.)
 
G

Gregory Brown

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top