Using OCRA, FXRuby and pdf-writer problem

Hello all,

  I made a simple salary calculator as practice using FXRuby, OCRA to
create an .exe file and pdf-writer to generate a small "report" with the
results of the calculation. The program runs fine when run with the
ruby interpreter, although I get some warnings:

C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/lang.rb:33:
warni
ng: method redefined; discarding old language=
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/strokestyle.rb:44
: warning: method redefined; discarding old cap=
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/strokestyle.rb:70
: warning: method redefined; discarding old join=
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:434:
warning:
method redefined; discarding old compressed=
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:471:
warning:
method redefined; discarding old absolute_left_margin
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:476:
warning:
method redefined; discarding old absolute_right_margin
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:481:
warning:
method redefined; discarding old absolute_top_margin
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:486:
warning:
method redefined; discarding old absolute_bottom_margin
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:492:
warning:
method redefined; discarding old margin_height
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:497:
warning:
method redefined; discarding old margin_width
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:502:
warning:
method redefined; discarding old absolute_x_middle
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:507:
warning:
method redefined; discarding old absolute_y_middle
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:512:
warning:
method redefined; discarding old margin_x_middle
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:517:
warning:
method redefined; discarding old margin_y_middle
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:526:
warning:
method redefined; discarding old y=
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:535:
warning:
method redefined; discarding old pointer=
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:1878:
warning:
method redefined; discarding old column_width
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:1885:
warning:
method redefined; discarding old column_gutter
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:1891:
warning:
method redefined; discarding old column_number
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:1897:
warning:
method redefined; discarding old column_count

Here is my code:
require 'fox16'
require 'pdf/writer'
include Fox

#crea PdfViewer
File.open('pdfviewer.bat','w') do |f| f.write '%1' end

class SueldoCalc < FXMainWindow
  def initialize(app)
    super(app,"SueldoCalc 1.0" , :opts=>
DECOR_TITLE|DECOR_BORDER|DECOR_CLOSE)

    @descuentos = Hash.new

    #seccion principal
    @packer = FXPacker.new(self, :opts=>LAYOUT_FILL)

    #seccion horas
    @seccion_horas = FXHorizontalFrame.new(@packer,:opts
=>LAYOUT_FILL_X|PACK_UNIFORM_WIDTH)
    @lblHoras = FXLabel.new(@seccion_horas,"Entre las horas
trabajadas:",:opts=>LAYOUT_FILL_X|JUSTIFY_LEFT)
    @txtHoras= FXTextField.new(@seccion_horas,20,:opts=>LAYOUT_FILL_X |
TEXTFIELD_INTEGER)

    #seccion rate
    @seccion_rate = FXHorizontalFrame.new(@packer,:opts
=>LAYOUT_FILL_X|PACK_UNIFORM_WIDTH)
    @lblRate = FXLabel.new(@seccion_rate,"Entre las
rate/hora:",:opts=>LAYOUT_FILL_X | JUSTIFY_LEFT)
    @txtRate= FXTextField.new(@seccion_rate,20,:opts=>LAYOUT_FILL_X |
TEXTFIELD_REAL )

    #seccion botones
    @botones = FXHorizontalFrame.new(@packer,:opts
=>LAYOUT_FILL_X|PACK_UNIFORM_WIDTH)
    @btnSubmit = FXButton.new(@botones,"Calcular", :opts=>
BUTTON_NORMAL|LAYOUT_FILL_X)
    @btnDesc =
FXButton.new(@botones,"Descuentos",:opts=>BUTTON_NORMAL|LAYOUT_FILL_X)
    @btnExit = FXButton.new(@botones,"Salir", :opts=>
BUTTON_NORMAL|LAYOUT_FILL_X)

    #eventos
    @btnSubmit.connect(SEL_COMMAND) do
      sueldo = @txtHoras.to_s.to_f * @txtRate.to_s.to_f

      #calcula descuentos
      total_descuentos = 0
      @descuentos.each_pair { |key,desc|
        cant_descontar = (desc/100) * sueldo
        total_descuentos = total_descuentos +cant_descontar
      }

      sueldo_neto = sueldo-total_descuentos

      pdf = PDF::Writer.new
      #pdf.select_font "Times-Roman"
      pdf.text "SueldoCalc 1.0", :justification =>
:center,:font_size=>50
      pdf.text "_" * 21

      pdf.text "Informe de cálculo", :justification =>
:center,:font_size=>24

      pdf.text "Sueldo Bruto: #{sprintf('%5.2f',sueldo)}",:font_size=>16
      pdf.text "Sueldo Neto: #{sprintf('%5.2f',sueldo_neto)}"
      pdf.text "Descuentos Aplicados:"
      #mensaje full
      #mensaje = "Sueldo Bruto: #{sprintf('%5.2f',sueldo)}\n"
      #mensaje = mensaje +"Sueldo Neto:
#{sprintf('%5.2f',sueldo_neto)}\n"
      #mensaje = mensaje + "Descuentos Aplicados:\n"

      @descuentos.each_pair { |key,value|
        pdf.text " "*5+key+": "+value.to_s+" \% \n"
      }
      pdf.text ' '
      pdf.text "Gracias por utlizar nuestro programa!", :justification
=> :center

      File.open("Sueldocalc.pdf","w") { |f| f.write pdf.render }

      system "PdfViewer.bat Sueldocalc.pdf"

      #FXMessageBox.information(self,MBOX_OK,"SueldoCalc",mensaje)

    end

    @btnExit.connect(SEL_COMMAND) do
      exit
    end

    @btnDesc.connect(SEL_COMMAND) do
      desc_form = Descuento.new(app,@descuentos)
      desc_form.create
    end

  end

  def create
    super
    show(PLACEMENT_SCREEN)
  end

end

class Descuento < FXMainWindow

  def Carga_Descuentos
    @lstDescuentos.clearItems()
    @descuentos.each_pair { |key,value| @lstDescuentos.appendItem( key +
': '+ value.to_s) }
  end

  def initialize(app,descuentos)
    super(app,"Descuentos",:opts=>DECOR_TITLE|DECOR_BORDER|DECOR_CLOSE)

    #trae los descuentos de la forma principal
    @descuentos = descuentos

    #frame principal (vertical)
    @main_frame =
FXVerticalFrame.new(self,:opts=>LAYOUT_FILL|PACK_UNIFORM_WIDTH)

    #seccion de añádir descuentos
    #seccion de escribir el nombre
    @add_desc_frame = FXHorizontalFrame.new(@main_frame,:opts=>
LAYOUT_FILL_X|PACK_UNIFORM_WIDTH)
    @lblNombre= FXLabel.new(@add_desc_frame,"Nombre de
descuento:",:opts=>LAYOUT_FILL_X|JUSTIFY_LEFT)
    @txtNombreDesc =
FXTextField.new(@add_desc_frame,20,:opts=>LAYOUT_FILL_X)

    #seccion de escribir el descuento
    @add_num_frame = FXHorizontalFrame.new(@main_frame,:opts=>
LAYOUT_FILL_X|PACK_UNIFORM_WIDTH)
    @lblCantidad= FXLabel.new(@add_num_frame,"Cantidad a
descontar:",:opts=>LAYOUT_FILL_X|JUSTIFY_LEFT)
    @txtCantidad =
FXTextField.new(@add_num_frame,20,:opts=>LAYOUT_FILL_X | TEXTFIELD_REAL)

    #sección botones
    @desc_botones =
FXHorizontalFrame.new(@main_frame,:opts=>LAYOUT_FILL|PACK_UNIFORM_WIDTH)
    @btnAdd=FXButton.new(@desc_botones,"Anadir",:opts=>LAYOUT_FILL_X|BUTTON_NORMAL)
    @btnDel=FXButton.new(@desc_botones,"Borrar",:opts=>LAYOUT_FILL_X|BUTTON_NORMAL)
    @btnReturn =
FXButton.new(@desc_botones,"Volver",:opts=>LAYOUT_FILL_X|BUTTON_NORMAL)

    #seccion lista
    @lblDesc=FXLabel.new(@main_frame,"Descuentos
actuales:",:opts=>JUSTIFY_LEFT)
    @lstDescuentos = FXList.new(@main_frame,:opts=>
LAYOUT_FILL_X|FRAME_SUNKEN )

    #eventos
    @btnReturn.connect(SEL_COMMAND) do
      destroy
    end

    @btnAdd.connect(SEL_COMMAND) do
      @descuentos [@txtNombreDesc.to_s] = @txtCantidad.to_s.to_f if
@txtNombreDesc.to_s.length >0 and @txtCantidad.to_s.to_f >0
      self.Carga_Descuentos
      @txtNombreDesc.text=''
      @txtCantidad.text=''
    end
  end

  def create
      super
      show(PLACEMENT_SCREEN)
    end

end

app=FXApp.new
sc = SueldoCalc.new(app)

app.create
app.run

···

--------------------------------

  I noticed that when I run this code with rubyw, it does not execute,
but with ruby.exe, it runs fine. Am I doing something wrong?
--
Posted via http://www.ruby-forum.com/.

Hello all,

I made a simple salary calculator as practice using FXRuby, OCRA to
create an .exe file and pdf-writer to generate a small "report" with the
results of the calculation. The program runs fine when run with the
ruby interpreter, although I get some warnings:

Warnings are normal,

[...]

When you want to attach an application code, use services like Pastie
or Gist from GitHub to avoid long emails...

--------------------------------

I noticed that when I run this code with rubyw, it does not execute,
but with ruby.exe, it runs fine. Am I doing something wrong?

For some reason your or one of the libraries is outputting to the
standard console.

RubyW is a windowless environment and thus, all puts and things will
fail because there is no standard console attached to it.

When you run it with ruby.exe, do you get all these warnings? I guess
not, but just to verify.

···

On Dec 9, 11:35 am, Omar Campos <hypermeis...@yahoo.com> wrote:

--
Luis Lavena

When you want to attach an application code, use services like Pastie
or Gist from GitHub to avoid long emails...

Ok, I'll keep that in mind. I'm still rather new to this forum. Where
can I find info about these services?

RubyW is a windowless environment and thus, all puts and things will
fail because there is no standard console attached to it.

When you run it with ruby.exe, do you get all these warnings? I guess
not, but just to verify.

Yes, the warnings appear only when using RubyW, or when running the .exe
made with OCRA with the script saved as .rb (which internally uses
ruby.exe, it seems). when running with RubyW, or the OCRA-made .exe
with script saved as .rbw, no warnings appear and the program simply
terminates.

···

--
Posted via http://www.ruby-forum.com/.

Hi again,

I did some more testing and found the following:

I commented out any lines that had to do with the pdf generation, ran
the app using RubyW and it worked. I then uncommented the the line:

require 'pdf/writer'

  Now, running with rubyw, nothing happens. It seems that something
inside pdf/writer does not let rubyw work properly. Have you, or anyone
else out there, encountered this problem?

···

--
Posted via http://www.ruby-forum.com/.

I think it is the warning messages that are causing the issue
They abort execution when using ruybw because stdout (or it maybe
stderr) doesn't exist
You may be able to redirect stdout and stderr to a file (or /dev/null)
before requiring the pdf lib so the messages have a valid place to go

cheers
Chris

···

On Dec 9, 10:17 pm, Omar Campos <hypermeis...@yahoo.com> wrote:

Hi again,

I did some more testing and found the following:

I commented out any lines that had to do with the pdf generation, ran
the app using RubyW and it worked. I then uncommented the the line:

require 'pdf/writer'

Now, running with rubyw, nothing happens. It seems that something
inside pdf/writer does not let rubyw work properly. Have you, or anyone
else out there, encountered this problem?
--
Posted viahttp://www.ruby-forum.com/.

Hi, I did as you said, and it worked. I put this line on top:

$stderr2 = $stderr.clone
$stderr.reopen('tmp2.dat')
require 'pdf/writer'

And then, when the script exits:

      @stderr.reopen($stderr2)
to restore the original output device. The reason for this is that when
I run OCRA, it runs the app to check its dependencies, but since there
is no output, it fails.

Many thanks to all who helped me with this issue.

···

--
Posted via http://www.ruby-forum.com/.

A bit off topic, but is there a reason why you're using PDF::Writer
instead of Prawn?

-greg

···

On Thu, Dec 10, 2009 at 10:41 PM, Omar Campos <hypermeister@yahoo.com> wrote:

Hi, I did as you said, and it worked. I put this line on top:

$stderr2 = $stderr.clone
$stderr.reopen('tmp2.dat')
require 'pdf/writer'