Forum: Ruby require 'diff/lcs/Array'

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.
Venkat K. (Guest)
on 2009-02-25 19:18
Hi All,

I need compare two text files. I should require 'diff/lcs/Array' when i
do this iam getting error. Should i install any additional gem(if yes
pls share the link). I am using Scite editor. As iam very new to ruby
kindly help me in this.

Thanks,
Venkat
James G. (Guest)
on 2009-02-25 19:56
(Received via mailing list)
On Feb 25, 2009, at 11:17 AM, Venkat K. wrote:

> I need compare two text files. I should require 'diff/lcs/Array'
> when i
> do this iam getting error.

I've tried using that library in the past, but its interface just
makes it crazy hard to work with.  Now I would just do something like:

   diff = `diff -u #{shell_escape path1} #{shell_escape path2} 2>&1`

I would define shell_escape() as something like:

   def shell_escape(str)
     String(str).gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/n, '\\').
                 gsub(/\n/,                                 "'\n'").
                 sub(/^$/,                                  "''")
   end


Hope that helps.

James Edward G. II
Daniel B. (Guest)
on 2009-02-25 21:45
(Received via mailing list)
On Feb 25, 10:55 am, James G. <removed_email_address@domain.invalid> wrote:
> On Feb 25, 2009, at 11:17 AM, Venkat K. wrote:
>
> > I need compare two text files. I should require 'diff/lcs/Array'  
> > when i
> > do this iam getting error.
>
> I've tried using that library in the past, but its interface just  
> makes it crazy hard to work with.

I came up with this for diff'ing 2 files and spitting the results to
stdout. It's meant to simulate "diff -u" output, but it's not quite
identical and is definitely not tested thoroughly:

# diff_test.rb
require 'diff/lcs'

# The file 'test1.txt' contains the following 3 lines:
#
# This is a house
# Hello World
# My favorite color is blue
#
# The file 'test2.txt' contains the following 3 lines:
#
# This is a car
# Hello World
# My favorite color is green

module Diff
   module LCS
      def self.diff_files(file1, file2)
         array1 = IO.readlines(file1)
         array2 = IO.readlines(file2)
         diffs = diff(array1, array2)

         print diffs.first[0].action * 3 + ' '
         print file1
         print "\t" + File.mtime(file1).to_s

         puts

         print diffs.first[1].action * 3 + ' '
         print file2
         print "\t" + File.mtime(file2).to_s

         puts

         diffs.each_with_index{ |diff, i|
            diff.each{ |d|
               puts d.action + d.element
            }
         }
      end
   end
end

Diff::LCS.diff_files('test1.txt', 'test2.txt')

Here was the output of Diff::LCS.diff_files:

--- test1.txt   Wed Feb 25 12:18:42 -0700 2009
+++ test2.txt   Wed Feb 25 12:18:53 -0700 2009
-This is a house
+This is a car
-My favorite color is blue
+My favorite color is green

Hope that was useful.

Regards,

Dan
Venkat K. (Guest)
on 2009-02-26 07:05
Hi ,

Thanks a lot for all ur replies.. My problem is I could't require
'diff/lcs'. I am getting an error message like.

c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require': no such file to load -- diff/lcs/Array
(LoadError)

Should I install any additional gem. kindly help me

Thanks,
venkat

Daniel B. wrote:
> On Feb 25, 10:55�am, James G. <removed_email_address@domain.invalid> wrote:
>> On Feb 25, 2009, at 11:17 AM, Venkat K. wrote:
>>
>> > I need compare two text files. I should require 'diff/lcs/Array' �
>> > when i
>> > do this iam getting error.
>>
>> I've tried using that library in the past, but its interface just �
>> makes it crazy hard to work with.
>
> I came up with this for diff'ing 2 files and spitting the results to
> stdout. It's meant to simulate "diff -u" output, but it's not quite
> identical and is definitely not tested thoroughly:
>
> # diff_test.rb
> require 'diff/lcs'
>
> # The file 'test1.txt' contains the following 3 lines:
> #
> # This is a house
> # Hello World
> # My favorite color is blue
> #
> # The file 'test2.txt' contains the following 3 lines:
> #
> # This is a car
> # Hello World
> # My favorite color is green
>
> module Diff
>    module LCS
>       def self.diff_files(file1, file2)
>          array1 = IO.readlines(file1)
>          array2 = IO.readlines(file2)
>          diffs = diff(array1, array2)
>
>          print diffs.first[0].action * 3 + ' '
>          print file1
>          print "\t" + File.mtime(file1).to_s
>
>          puts
>
>          print diffs.first[1].action * 3 + ' '
>          print file2
>          print "\t" + File.mtime(file2).to_s
>
>          puts
>
>          diffs.each_with_index{ |diff, i|
>             diff.each{ |d|
>                puts d.action + d.element
>             }
>          }
>       end
>    end
> end
>
> Diff::LCS.diff_files('test1.txt', 'test2.txt')
>
> Here was the output of Diff::LCS.diff_files:
>
> --- test1.txt   Wed Feb 25 12:18:42 -0700 2009
> +++ test2.txt   Wed Feb 25 12:18:53 -0700 2009
> -This is a house
> +This is a car
> -My favorite color is blue
> +My favorite color is green
>
> Hope that was useful.
>
> Regards,
>
> Dan
Daniel B. (Guest)
on 2009-02-26 17:01
(Received via mailing list)
On Feb 25, 10:04 pm, Venkat K. <removed_email_address@domain.invalid> wrote:
> Hi ,
>
> Thanks a lot for all ur replies.. My problem is I could't require
> 'diff/lcs'. I am getting an error message like.
>
> c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
> `gem_original_require': no such file to load -- diff/lcs/Array
> (LoadError)
>
> Should I install any additional gem. kindly help me

Something may have gone wrong with your gem install. I would try
uninstalling diff-lcs and reinstalling it.

You haven't installed it manually also, have you? Maybe you have it in
your sitelib dir as well, and it's missing there.

Regards,

Dan
Suraj K. (Guest)
on 2009-02-27 00:06
James G. wrote:
> I would define shell_escape() as something like:
>
>    def shell_escape(str)
>      String(str).gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/n, '\\').
>                  gsub(/\n/,                                 "'\n'").
>                  sub(/^$/,                                  "''")
>    end
>

Nice!  I always wished this method was in the Ruby stdlib.  Maybe it
could be added as a class method of the Shell class in the "shell"
stdlib?

  require 'shell'
  Shell.escape('foo "bar".txt')

Shall I file a feature request to ruby-core?
James G. (Guest)
on 2009-02-27 00:12
(Received via mailing list)
On Feb 26, 2009, at 4:05 PM, Suraj K. wrote:

> Nice!  I always wished this method was in the Ruby stdlib.  Maybe it
> could be added as a class method of the Shell class in the "shell"
> stdlib?

It's in Ruby 1.9.  See my notes at the bottom of this post:

http://blog.grayproductions.net/articles/the_secre...

James Edward G. II
This topic is locked and can not be replied to.