Saari Development

Ali Rizvi’s Technical Blog as a Professional Software Development Engineer

Archive for the ‘code’ Category

Java: Rediscovering the basics

without comments

It’s been a while since I did some Java but now it seems to be the right tool for the job at hand so I am rediscovering Java with a masochistic twist, ‘look boss no IDEs’.

I am experimenting with the SSJ Java library for Stochastic Simulation as I mentioned in an earlier post and here is some sample source code:

import umontreal.iro.lecuyer.randvar.*;
import umontreal.iro.lecuyer.probdist.*;
import umontreal.iro.lecuyer.rng.*; 

public class GenerateDemandArrivals {
  private RandomStream randomStream;
  private RandomVariateGen expArrival;
  private RandomVariateGen poissonArrival;
  private double lambda;
  private int times;

  public GenerateDemandArrivals() {
    randomStream = new MRG32k3a();
    lambda = 10.0;
    times = 5;
    expArrival = new RandomVariateGen(randomStream, new ExponentialDist(lambda));
    poissonArrival = new PoissonGen(randomStream, new PoissonDist(lambda));

  }

  public void demandArrival() {
    for (int i = 0; i < times; i++) {
      System.out.println("Here is the next random int : " + randomStream.nextInt(0, 10));
    }
  }

  public void demandArrivalExpDist() {
    for (int i = 0; i < times; i++) {
      System.out.println("Here is the next exponential dist double : " + expArrival.nextDouble());
    }
  }

  public void demandArrivalPoissonDist() {
    for (int i = 0; i < times; i++) {
      System.out.println("Here is the next poisson dist double : " + poissonArrival.nextDouble());
    }
  }

  public static void main(String[] arg) {
    new GenerateDemandArrivals().demandArrival();
    new GenerateDemandArrivals().demandArrivalExpDist();
    new GenerateDemandArrivals().demandArrivalPoissonDist();

  }
}

I was tried of doing the following everytime I changed the source code in my favorite editor Vim:

javac -cp "C:\Program Files\Java\jdk1.6.0_02\lib\ext\ssj.jar;." GenerateDemandArrivals.java
java -cp "C:\Program Files\Java\jdk1.6.0_02\lib\ext\ssj.jar;." GenerateDemandArrivals

I already had ant installed on my Windows machine but so I said why not take advantage of it, I looked for simple ant file and found something quite close in apache ant manual.

After little bit more search and refinement here is what I came up with:

<project name="Sim" default="run" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="."/>
  <property name="build" location="."/>
  <path id="project.class.path">
    <pathelement path="C:\Program Files\Java\jdk1.6.0_02\lib\ext\ssj.jar"/>
    <pathelement path="."/>
  </path>

  <target name="run"
    description="run the program"
    depends="compile">
    <java classname="GenerateDemandArrivals">
      <classpath refid="project.class.path"/>
    </java>
  </target>

  <target name="compile"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}">
    <classpath refid="project.class.path"/>
    </javac>
  </target>

</project>

Now I can simple type ant (while I am in the directory containing the build.xml file above) after any changes to the file and it compiles and run my code.

I feeling ashamed of feeling good and writing about such basic stuff but I am thinking there would be another person like me (could be myself at some future point where I have to leave my lovely Ruby world and come back to Java out of necessity).

Written by imsaar

November 20, 2008 at 7:06 pm

Posted in code, java

Tagged with , , , ,

JRuby: Stochastic Simulation with SSJ

with 2 comments

I needed to work on a simulation project that involved working with the SSJ java library.
If I haven’t mentioned before I love Ruby (the programming language) and Java, though a tool in my toolkit, does not evoke the same feeling.

So my first reaction was I can use JRuby to benefit from the existing java library and beauty of Ruby.
The SSJ library comes with a comprehensive sets of examples and documentation. I started exploring that.

Fortunately at RubyConf 2008 with some help from Charles Nutter I was able to install jruby on my laptop, getting jruby installed and running was not trivial. I wish somebody can do a one click installer for jruby same as they did for Ruby and Rails.

I downloaded SSJ and unzipped the downloaded file containing jar files into C:\Program Files\Java\jdk1.6.0_02\lib\ext directory. Added the CLASSPATH variable in my windows environment set to this directory.

I tried the following example from examples.pdf

import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.stat.*;
public class Collision {
    int k; // Number of locations.
    int m; // Number of items.
    double lambda; // Theoretical expectation of C (asymptotic).
    boolean[] used; // Locations already used.
    public Collision (int k, int m) {
        this.k = k;
        this.m = m;
        lambda = (double) m * m / (2.0 * k);
        used = new boolean[k];
    }
    // Generates and returns the number of collisions.
    public int generateC (RandomStream stream) {
        int C = 0;
        for (int i = 0; i < k; i++) used[i] = false;
        for (int j = 0; j < m; j++) {
            int loc = stream.nextInt (0, k-1);
            if (used[loc]) C++;
            else used[loc] = true;
        }
        return C;
    }
    // Performs n indep. runs using stream and collects statistics in statC.
    public void simulateRuns (int n, RandomStream stream, Tally statC) {
        statC.init();
        for (int i=0; i<n; i++) statC.add (generateC (stream));
        statC.setConfidenceIntervalStudent();
        System.out.println (statC.report (0.95, 3));
        System.out.println (" Theoretical mean: " + lambda);
    }
    public static void main (String[] args) {
        Tally statC = new Tally ("Statistics on collisions");
        Collision col = new Collision (10000, 500);
        col.simulateRuns (100000, new MRG32k3a(), statC);
    }
}

I compiled and ran the Java code above to ensure I was getting the same results using the following steps:

javac -cp "C:\Program Files\Java\jdk1.6.0_02\lib\ext\ssj.jar" Collision.java
java -cp "C:\Program Files\Java\jdk1.6.0_02\lib\ext\ssj.jar;C:\code\ruby\jruby" Collision

As you can notice my code was in the C:\code\ruby\jruby directory and I was in this directory when I ran the above program.

Here is the same code example in jruby code:

require 'java'
require 'ssj.jar'

import 'umontreal.iro.lecuyer.rng.RandomStream'
import 'umontreal.iro.lecuyer.stat.Tally'
import 'umontreal.iro.lecuyer.rng.MRG32k3a'

class Collision
  def initialize(k, m)
    @k = k
    @m = m
    @lambda = m * m / (2.0 * k)
    @used = Array.new(k, false)
  end

  def generate_c(stream)
    c = 0
    @k.times { |i| @used[i] = false }
    @m.times do |j|
      loc = stream.nextInt(0, @k-1)
      if @used[loc]
        c += 1
      else
        @used[loc] = true
      end
    end
    return c
  end

  def simulate_runs(n, stream, stat_c)
   stat_c.init
   n.times { stat_c.add(generate_c(stream)) }
   stat_c.setConfidenceIntervalStudent()
   puts stat_c.report(0.95, 3)
   puts " Theoretical mean: #{@lambda} "
  end

  def self.run
    stat_c = Tally.new("Statistics on collision")
    col = Collision.new(10000,500)
    col.simulate_runs(100000, MRG32k3a.new, stat_c)
  end

end

Collision.run

The jruby code runs comparatively slower (my feeling is 5 times slower but I will measure and add it later). I am using the latest available jruby 1.1.5 at the time of this post.

Update: The JRuby version takes around 13 minutes while the equivalent Java code above takes 17 seconds. This seems to be unacceptable performance, I am not sure if this is specific to SSJ or other libraries too. If I understand correctly, JRuby should be running inside the JVM and when I call SSJ library that is the same Java code that runs so I don’t understand why it is so slow unless I have used some technique that slows down my JRuby version.

Written by imsaar

November 15, 2008 at 10:25 pm

Posted in code, java, jruby, ruby

Ruby : Deep Copy

with one comment

# problem
a = [[0, 1], [10], 19]
b = a.dup
b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 100], [10], 19]

# solution
a = [[0, 1], [10], 19]
b = Marshal.load(Marshal.dump(a))
b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 1], [10], 19]

Written by imsaar

January 9, 2008 at 10:13 pm

Posted in code, ruby

Ruby: Grid Computing Golf

with 2 comments

My first attempt at really playing code golf, that is solving a problem in minimum characters of code and I did not do very well (200 character compared to the best 43 perl or 63 ruby characters).
http://codegolf.com/grid-computing

m = []
r = []
c = Array.new(10, 0)
0.upto(9) {|n|m<<gets.chomp.split.map {|x|x.to_i};r<<m[n].inject(0){|s,e| s += e}}
0.upto(9) {|x|m.each_with_index{|v,z|c[x] += v[x]}}
puts [r.max, c.max].max

Update:
I further squeezed some white-space out of the code and it is starting to look uglier (now it is 184 characters). I am not sure I like the code golf concept although I like to solve coding problems.

m=[]
r=[]
c=Array.new(10,0)
0.upto(9){|n|m<<gets.chomp.split.map{|x|x.to_i};r<<m[n].inject(0){|s,e|s+=e}}
0.upto(9){|x|m.each_with_index{|v,z|c[x]+=v[x]}}
puts [r.max,c.max].max

Written by imsaar

January 8, 2008 at 1:10 am

Posted in code, golf, ruby

Ruby: Oblongular Number Spirals Solution

without comments

<pre>
  # Oblongular Number Spirals
  # http://codegolf.com/oblongular-number-spirals

  class Spiral
    attr_accessor :row_num, :col_num, :direction

    def initialize(m, n)
      @spiral = []
      @value =
     @row_num = m
     @col_num = n
     @direction = :right
   end

   def get(row, col)
     if (row = row_num || col = col_num)
         raise RangeError.new("Out of Bound : row = #{row} col = #{col}")
     end
     @spiral[row] ||= Array.new
     @spiral[row][col] ||= nil
     @spiral[row][col]
   end

   def increment_fill(row, col)
     @value +=
     fill(row, col, @value)
   end

   def fill(row, col, value)
     if (row = row_num || col = col_num)
       raise RangeError.new("Out of Bound : row = #{row} col = #{col}")
     end
     @spiral[row] ||= Array.new
     @spiral[row][col] = value
   end

   def populate(x =, y =)
     count =
     while ()
       increment_fill(x, y)
       count +=
       # exit the loop once iteration == total_cells
       break if count == row_num * col_num
       x, y = next_cell(x, y)
     end
   @spiral
   end

   def next_cell(row, col)
     new_row = row
     new_col = col

     case @direction
     when :right
       new_col = col + 1
     when :left
       new_col = col - 1
     when :down
       new_row = row + 1
     when :up
       new_row = row - 1
     end

     if ((new_col >= col_num || new_col = row_num || new_row < 0))
       change_direction
       next_cell(row, col)
     elsif (get(new_row, new_col) != nil)
       change_direction
       next_cell(row, col)
     else
       return [new_row, new_col]
     end
   end

   def change_direction
     case @direction
     when :right
       @direction = :down
     when :down
       @direction = :left
     when :left
       @direction = :up
     when :up
       @direction = :right
     else
       raise ArgumentError.new("Illegal value  for current_direction #{@direction}")
     end
   end

   def dump
    display = ''
    @spiral.each do |row|
      row.each do |col|
        display << sprintf("%d ", col)
      end
      display << "\n"
    end
    puts display
   end
 end

 if $ == __FILE__
   s = Spiral.new(ARGV[].to_i, ARGV[].to_i)
   s.populate
   s.dump
 end

# test file below
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')

  require 'test/unit'
  require 'spiral'

  class TestSpiral &lt; Test::Unit::TestCase
    def setup
      @spiral = Spiral.new(,)
    end

   def test_increment_fill_0_0_first
     @spiral.increment_fill(0, 0)
     assert_equal(1, @spiral.get(0, 0))
   end

   def test_increment_fill_1_1_first
     @spiral.increment_fill(1, 1)
     assert_equal(1, @spiral.get(1, 1))
   end

   def test_increment_fill_successive
     @spiral.increment_fill(0, 0)
     @spiral.increment_fill(0, 1)
     @spiral.increment_fill(1, 1)
     assert_equal(3, @spiral.get(1, 1))
   end

   def test_get_beyond_limit_raises_exception
     assert_raise(RangeError) {
       @spiral.get(@spiral.row_num + 1, @spiral.col_num + 1)
     }
   end

   def test_get_below_zero_raises_exception
     assert_raise(RangeError) {
       @spiral.get(-,)
     }
   end

   def test_get_at_limit_raises_exception
     assert_raise(RangeError) {
       @spiral.get(@spiral.row_num, @spiral.col_num)
     }
   end

   def test_unfill_get_with_in_range_does_not_raise_exception
     assert_nothing_raised() {
       @spiral.get(@spiral.row_num -, @spiral.col_num - 1)
     }
   end

   def test_unfill_get_with_in_range_returns_nil
     assert_equal(nil, @spiral.get(@spiral.row_num - 1, @spiral.col_num - 1))
   end

   def test_fill_beyond_range_raises_exception
     assert_raise(RangeError) {
       @spiral.increment_fill(@spiral.row_num + 1, @spiral.col_num + 1)
     }
   end

   def test_fill_beyond_range_raises_exception
     assert_raise(RangeError) {
       @spiral.increment_fill(0, -1)
     }
   end

   def test_fill_at_range_raises_exception
     assert_raise(RangeError) {
       @spiral.increment_fill(@spiral.row_num, @spiral.col_num)
     }
   end

   def test_populate
     spiral = [
           [1, 2, 3, 4, 5],
           [14, 15, 16, 17, 6],
           [13, 20, 19, 18, 7],
           [12, 11, 10, 9, 8]
          ]
     s = @spiral.populate
     assert_equal(spiral, s)
   end

   def test_change_directon_right
     @spiral.direction = :right
     @spiral.change_direction
     assert_equal(:down, @spiral.direction)
   end

   def test_change_directon_up
     @spiral.direction = :up
     @spiral.change_direction
     assert_equal(:right, @spiral.direction)
   end

   def test_next_cell_0_0
     x, y = @spiral.next_cell(0,0)
     assert_equal(0, x)
     assert_equal(1, y)
   end

   def test_next_cell_0_4
     x, y = @spiral.next_cell(0,4)
     assert_equal(1, x)
     assert_equal(4, y)
   end

 end
</pre>

Written by imsaar

January 4, 2008 at 8:11 pm

Posted in code, ruby

Ruby : Time Math Interview Problem With Bug Fixed (still writing test first)

with one comment

While discussing my friend Arsalan’s C# (seemlessly compiled on my linux machine using mcs)
solution and testing it out I found a bug in my own code that. The problem was when adding
more than 12 hours (> 720 minutes) it was not doing the right thing. The code can still be
refactored for cleaner solution but it is too late at night to do that now. Also, my wife
gave me another idea to convert the time to minutes before adding which I will try out later.

   # Without using any built in date or time functions, write a function or method
   # that accepts two mandatory arguments. The first argument is a string of the
   # format "[H]H:MM {AM|PM}" and the second argument is an integer. Assume the
   # integer is the number of minutes to add to the string. The return value of
   # the function should be a string of the same format as the first argument.
   # For example AddMinutes(" AM",) would return " AM". The exercise
   # isn't meant to be too hard. I just want to see how you code. Feel free to
   # do it procedurally or in an object oriented way, whichever you prefer. Use
   # any language you want. Write production quality code.
  # Question Source: http://blist.com/blog/

  # the following solution was developed using TDD

  require 'test/unit'

  class TestTimeCalc < Test::Unit::TestCase

      def setup
          @time = " AM"
      end

      def test_new_time_cal
          assert_not_nil(TimeCalc.new)
      end

      def test_add_minute_zero
          assert_equal(@time, TimeCalc.add_minutes(@time,))
      end

      def test_add_minute_ten
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_minute_thirteen
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_hour
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_two_hours_fifteen_minutes
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_past_meridiem
          # minutes = hours and minutes
          assert_equal(" PM", TimeCalc.add_minutes(@time,))
      end

      def test_alpha_hour_min_format_throws_exception
          assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD AM",) }
      end

      def test_bad_meridiem_throws_exception
          assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD TM",) }
      end

      def test_hr_greater_than_twelve
          assert_raise(ArgumentError) { TimeCalc.add_minutes(" PM",) }
      end

      def test_min_greater_than_fifty_nine
          assert_raise(ArgumentError) { TimeCalc.add_minutes(" PM",) }
      end

      def test_add_lot_of_minutes
        # minutes = hours and minutes
        assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_up_to_noon
        # AM plus hr min ()
        assert_equal(" PM", TimeCalc.add_minutes(@time,))
      end

      def test_add_whole_lot_of_minutes
        # minutes = hours and minutes
        assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

  end # end class TestTimeCalc

  class TimeCalc

      def self.add_minutes(time, minutes)
        (hour, min, meridiem) = parse_time_string(time)

        hour_increment = (min + minutes)/
        min_increment = (min + minutes)% - min

        while (hour + hour_increment >)
          meridiem = (meridiem == 'AM' ? 'PM' : 'AM')
          hour_increment -=
        end

        hour += hour_increment
        # special case forth hour
        meridiem = (meridiem == 'AM' ? 'PM' : 'AM') if hour ==
        min += min_increment

        hour.to_s + ":" + sprintf('%d', min) + " " + meridiem
      end

      private

      def self.parse_time_string(time)
          raise ArgumentError unless (matches = time.match(/(\d{,}):(\d{,})\s+(\w{})/))
          matches = time.match(/^(\d{,}):(\d{,})\s+([A|P]M)$/)
          hour = matches[].to_i
          min = matches[].to_i
          meridiem = matches[]
          raise ArgumentError unless (hour <=)
          raise ArgumentError unless (min <)
          return [hour, min, meridiem]
      end
  end # end class TimeCalc

 if __FILE__ == $
   puts TimeCalc.add_minutes(ARGV[], ARGV[].to_i)
 end

Written by imsaar

January 2, 2008 at 9:33 am

Posted in code, ruby

Ruby: One liner to strip the line number from code posted

without comments

In response to my previous post my friend Arsalan posted his C# solution on my shared friend blog SaarayDost

Both my solution and his had line numbers with the code which makes it easy to reference a line number in a discussion but if you want to run and test code it can be painful to strip them out one by one.

I wrote the following quick ruby one liner that can be run on a file after copy-pasting the code and saving it.

ruby -lne 'puts $_.gsub(/^\s?\d+:?/, "")' ali.rb > ali_fixed.rb

I am sure there good be a better more clever way to do this after all TIMTOWDI but here I use the l to remove newline at the end of each line, n to iterate over the given file one line at a time and e to execute the following code string (in quotes, on each line). $_ is the magic variable (a perl legacy in ruby) that magically populated, in this case with the current line of file being processed. gsub simply removed the pattern match in the first argument by the string in the second argument.

While searching for a good description of $_ I also found this interesting link with other useful ruby one-liners.

Update: found another way to achieve the above result. -p option prints the $_ at the end of each iteration.

ruby -lpe '$_.gsub!(/^\s?\d+:?/, "")' ali.rb > ali_fixed.rb

Written by imsaar

December 31, 2007 at 4:02 am

Posted in code, ruby

Ruby : Time Math Interview Problem Done Test First

with 3 comments

Question from : Blist.com Career Page

 # Without using any built in date or time functions, write a function or method
 # that accepts two mandatory arguments. The first argument is a string of the
 # format "[H]H:MM {AM|PM}" and the second argument is an integer. Assume the
 # integer is the number of minutes to add to the string. The return value of
 # the function should be a string of the same format as the first argument.
 # For example AddMinutes("9:13 AM", 10) would return "9:23 AM". The exercise
 # isn't meant to be too hard. I just want to see how you code. Feel free to
 # do it procedurally or in an object oriented way, whichever you prefer. Use
 # any language you want. Write production quality code.
 # Question Source: http://blist.com/blog/

 # the following solution was developed using TDD

 require 'test/unit'

 class TestTimeCalc < Test::Unit::TestCase

     def setup
         @time = "9:13 AM"
     end

     def test_new_time_cal
         assert_not_nil(TimeCalc.new)
     end

     def test_add_minute_zero
         assert_equal(@time, TimeCalc.add_minutes(@time, 0))
     end

     def test_add_minute_ten
         assert_equal("9:23 AM", TimeCalc.add_minutes(@time, 10))
     end

     def test_add_minute_thirteen
         assert_equal("9:26 AM", TimeCalc.add_minutes(@time, 13))
     end

     def test_add_hour
         assert_equal("10:13 AM", TimeCalc.add_minutes(@time, 60))
     end

     def test_add_two_hours_fifteen_minutes
         assert_equal("11:28 AM", TimeCalc.add_minutes(@time, 135))
     end

     def test_add_past_meridiem
         # 785 minutes = 13 hours and 5 minutes
         assert_equal("10:18 PM", TimeCalc.add_minutes(@time, 785))
     end

     def test_alpha_hour_min_format_throws_exception
         assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD AM", 10) }
     end

     def test_bad_meridiem_throws_exception
         assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD TM", 10) }
     end

     def test_hr_greater_than_twelve
         assert_raise(ArgumentError) { TimeCalc.add_minutes("13:00 PM", 10) }
     end

     def test_min_greater_than_fifty_nine
         assert_raise(ArgumentError) { TimeCalc.add_minutes("12:60 PM", 10) }
     end

 end # end class TestTimeCalc

 class TimeCalc

     def self.add_minutes(time, minutes)
       (hour, min, meridiem) = parse_time_string(time)

       hour_increment = (min + minutes)/60
       min_increment = (min + minutes)%60 - min
       if (hour_increment >= 12)
         meridiem = (meridiem == 'AM' ? 'PM' : 'AM')
         hour_increment -= 12
       end

       hour += hour_increment
       min += min_increment

       hour.to_s + ":" + min.to_s + " " + meridiem
     end

     private

     def self.parse_time_string(time)
         raise ArgumentError unless (matches = time.match(/(\d{1,2}):(\d{1,2})\s+(\w{2})/))
         matches = time.match(/^(\d{1,2}):(\d{1,2})\s+([A|P]M)$/)
         hour = matches[1].to_i
         min = matches[2].to_i
         meridiem = matches[3]
         raise ArgumentError unless (hour <= 12)
         raise ArgumentError unless (min < 60)
         return [hour, min, meridiem]
     end

 end # end class TimeCalc

Written by imsaar

December 28, 2007 at 11:18 pm

Posted in code, ruby

Ruby: net/smtp weirdness – to address can’t contain dash/hyphen (-)

without comments

This caused me a lot of grief and debugging time.

I am using code like this to send emails from a CGI script:

    Net::SMTP.start('smtp.example.com', 25) do |smtp|
      smtp.open_message_stream('from@example.com', ['dest-address@example.com']) do |f|
        f.puts 'From: from@example.com'
        f.puts 'To: dest-address@example.com'
        f.puts 'Cc: cc-address@example.com'
        f.puts 'Subject: test message'
        f.puts
        f.puts 'This is a test message.'
      end
    end

and this is failing silent in my case.

I have discovered through trial and error that if I remove the -(dash) from the to address it works. Dashes in from or CC addresses does not matter.

Written by imsaar

October 3, 2007 at 12:15 am

Posted in code, ruby

Ruby: Rotating Image Files Code

with one comment

require 'rmagick'
require 'exifr'
require 'pp'

ROTATE_CLOCKWISE = 90
ROTATE_ANTICLOCKWISE = -90

def rotate(file)
  image_exif = EXIFR::JPEG.new(file).exif

  puts "#{file} : #{image_exif[:orientation]}"
  if (image_exif[:orientation] == EXIFR::TIFF::RightTopOrientation)
    degrees = ROTATE_CLOCKWISE
  elsif (image_exif[:orientation] == EXIFR::TIFF::LeftBottomOrientation)
    degrees = ROTATE_ANTICLOCKWISE
  else
    puts "Not rotating #{file}"
    return
  end

  image = (Magick::Image.read(file)).first
  #image.rotate(degrees).write("#{file.split('.').first}_rotated.jpg")
  image.rotate(degrees).write("#{file}")
end

dir = ARGV[0]

Dir.entries(dir).each do |file|
  if (file =~ /jpg/i)
    rotate(File.join(dir, file))
  end
end

Written by imsaar

June 25, 2007 at 12:43 pm

Posted in code, ruby