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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2008-12-10 18:12
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-12-10 21:50
(Received via mailing list)
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
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2008-12-10 22:33
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
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2008-12-10 22:43
(Received via mailing list)
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).
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2008-12-11 03:30
(Received via mailing list)
On Wed, Dec 10, 2008 at 4:35 PM, Matthew Moss <matt@moss.name> 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
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2008-12-11 04:07
Greg

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

MC
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2008-12-11 04:36
(Received via mailing list)
On Dec 10, 2008, at 8:23 PM, Gregory Brown wrote:

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

Yes, that.  :D
Efdb04c4dac4a962d86e25361f8fd450?d=identicon&s=25 James M. Lawrence (quix)
on 2008-12-11 04:59
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.)
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2008-12-11 15:57
(Received via mailing list)
On Wed, Dec 10, 2008 at 10:53 PM, James M. Lawrence
<quixoticsycophant@gmail.com> 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
This topic is locked and can not be replied to.