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
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
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
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).
On Wed, Dec 10, 2008 at 4:35 PM, Matthew M. [email protected] 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
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. 
Greg
I spent all day on this. You have really helped me! Thanks so very
much!!!
MC
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.)
On Wed, Dec 10, 2008 at 10:53 PM, James M. Lawrence
[email protected] 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