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.
1c0045cd0f1d26711356edb37b3f14b1?d=identicon&s=25 Venkat Kumar (venkat5)
on 2009-02-25 18: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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-02-25 18:56
(Received via mailing list)
On Feb 25, 2009, at 11:17 AM, Venkat Kumar 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 Gray II
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (djberg96)
on 2009-02-25 20:45
(Received via mailing list)
On Feb 25, 10:55 am, James Gray <ja...@grayproductions.net> wrote:
> On Feb 25, 2009, at 11:17 AM, Venkat Kumar 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
1c0045cd0f1d26711356edb37b3f14b1?d=identicon&s=25 Venkat Kumar (venkat5)
on 2009-02-26 06: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 Berger wrote:
> On Feb 25, 10:55�am, James Gray <ja...@grayproductions.net> wrote:
>> On Feb 25, 2009, at 11:17 AM, Venkat Kumar 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
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (djberg96)
on 2009-02-26 16:01
(Received via mailing list)
On Feb 25, 10:04 pm, Venkat Kumar <venkatesh...@gmail.com> 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
Bc368ef524130e8d0deb386de961e24a?d=identicon&s=25 Suraj Kurapati (snk)
on 2009-02-26 23:06
James Gray 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?
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-02-26 23:12
(Received via mailing list)
On Feb 26, 2009, at 4:05 PM, Suraj Kurapati 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 Gray II
This topic is locked and can not be replied to.