From python to ruby

Hello

object :
+++++

I never made a script with Ruby (and with python !).
I would like to translate a little script from python to ruby to insert
it
inside a bundle made with ruby and I would like with this try to work
a little with ruby. I work with OS X and I have ruby 1.8.7.

the script in python :
++++++++++++++++++


#!/usr/bin/env

python -u

-- coding: utf-8 --

import glob

HEADER = r"""\input{Vorspann.ltx}

\begin{document}

%s
\end{document}
“”"

texFiles = glob.glob(’*.tex’)

for fname in texFiles[:]:
#print fname

text = file(fname, 'rb').read()

text2 = HEADER % text

fh = file('new/%s' % fname, 'wb')
fh.write(text2)
fh.close()

Explanation

:
+++++++++

The script is in a folder F with text (here it’s tex) files (utf8 files)
The script reads the LaTeX files inside F , then
it adds the line : \input{preamble.ltx}

and to finish it puts around the initial lines two lines:

\begin{document}

\end{document}

Example :
+++++++

test.tex is a text file which contains :

I’m a file test.

the result is a file with the same name in a subfolder named “new”
and text.tex now contains ::

\input{preamble.ltx}
\begin{document}
I’m a file test.
\end{document}

I hope someone can help me

Thanks and best regards

AM

PS : What is the better book to learn how to script with ruby ?

i think there is a book named “Everyday Scripting with Ruby”.

Check fileutils in rubydocs (dir and file operations).

See this:

You can also look at gems such as highline, thor, boson, main, gli and
vision commander.

On 2010-12-29 14:55:25 +0100, Rahul K. said:

i think there is a book named “Everyday Scripting with Ruby”.

Check fileutils in rubydocs (dir and file operations).

See this:
How do I use Ruby for shell scripting? - Stack Overflow

You can also look at gems such as highline, thor, boson, main, gli and
vision commander.

Thanks for your answer.

I get the book named “Everyday Scripting with Ruby” but I am a real
beginner
so I need to learn a lot of things before to build a script.

Thanks for the link too

AM

I am not sure of your level when you say “real beginner”. Do you mean
beginner in programming or in ruby ?

Perhaps you can start with ruby tutorials. Ruby is not very different
from python. The tutorials (ruby-lang.org) should get you comfortable
with ruby syntax, control flow and other basics.

Hope this helps.

On Wed, Dec 29, 2010 at 5:05 PM, Alain M. [email protected] wrote:

I get the book named “Everyday Scripting with Ruby” but I am a real
beginner
so I need to learn a lot of things before to build a script.

A beginner in programming or with Ruby/Python?

In the first case, I’d suggest Chris P.'s excellent Learn To
Program, which is an expanded version of this website:
http://pine.fm/LearnToProgram/

Coincidentally, Mr Pine saw fit to teach programming with the help of
Ruby. It’s a two for one deal for you. :wink:

Then you should get the also excellent PickAxe (aka Programming Ruby)
by Dave T. (et al. by now, I guess).

In the second case, just get the PickAxe and the
excellent-by-reputation (I don’t own a copy myself) The Ruby
Programming Language by our own Matz.

And books of general interest: Hal F.'s (et al.) The Ruby Way is a
good buy, and so seems to be The Ruby Cookbook and Ruby Best Practices
(the book’s available for free, but I’m sure Gregory would appreciate
if you got the deadtree edition, too :wink: ) and so is Agile Web
Development with Rails (if you want to dip your toes into those
waters, anyway)

Hope this helps you a bit. :slight_smile:


Phillip G.

Though the folk I have met,
(Ah, how soon!) they forget
When I’ve moved on to some other place,
There may be one or two,
When I’ve played and passed through,
Who’ll remember my song or my face.

On Dec 28, 9:32am, AM [email protected] wrote:

the script in python :
HEADER = r"“”\input{Vorspann.ltx}
#print fname
Explanation
\begin{document}
the result is a file with the same name in a subfolder named “new”

AM

PS : What is the better book to learn how to script with ruby ?

require ‘ftools’

HEADER = '\input{Vorspann.ltx}

\begin{document}

%s
\end{document}

File.makedirs( “new” )

Dir[ “*.tex” ].each{|fname|
text = IO.read( fname )
text2 = HEADER % text
File.open( “new/” + fname, “wb” ){|fh|
fh.print text2
}
}

On Tue, Dec 28, 2010 at 9:35 AM, AM [email protected] wrote:

PS : What is the better book to learn how to script with ruby ?

For Theory:

The Well-Grounded Rubyist
David A. Black
ISBN: 1933988657

For Depth:

Metaprogramming Ruby: Program Like the Ruby Pros
Paolo Perrotta
ISBN: 978-1-93435-647-0

For Reference:

The Ruby P.ming Language
Yukihiro M…
ISBN: 0596516177

On 2010-12-29 17:28:08 +0100, Phillip G. said:

A beginner in programming or with Ruby/Python?

Hello Phillip,
I’m a beginner with Ruby/Python. I can understand the code, read the
code but I’m not able to build something actually.
I learned Pascal :frowning:
I’m a beginner with object-oriented programming language

In the first case, I’d suggest Chris P.'s excellent Learn To
Program, which is an expanded version of this website:
Learn to Program, by Chris Pine

Coincidentally, Mr Pine saw fit to teach programming with the help of
Ruby. It’s a two for one deal for you. :wink:

Thanks I bought this book but I only read the first chapters. I like
this book
anf the way use by Chris P. to teach Ruby. (sorry for my english
language)

Then you should get the also excellent PickAxe (aka Programming Ruby)
by Dave T. (et al. by now, I guess).

In the second case, just get the PickAxe and the
excellent-by-reputation (I don’t own a copy myself) The Ruby
Programming Language by our own Matz.

Ok but PickAxe covers 1.9 or I’ve 1.8.7 with OS X. Is this a problem?
I’m afraid to install 1.9 because I work with TextMate and this soft is
very
dependent of Ruby.

And books of general interest: Hal F.'s (et al.) The Ruby Way is a
good buy, and so seems to be The Ruby Cookbook and Ruby Best Practices
(the book’s available for free, but I’m sure Gregory would appreciate
if you got the deadtree edition, too :wink: ) and so is Agile Web
Development with Rails (if you want to dip your toes into those
waters, anyway)

Hope this helps you a bit. :slight_smile:

Thanks for all your suggestions

AM

On 2010-12-29 22:05:23 +0100, w_a_x_man said:

File.makedirs( “new” )

Dir[ “*.tex” ].each{|fname|
text = IO.read( fname )
text2 = HEADER % text
File.open( “new/” + fname, “wb” ){|fh|
fh.print text2
}
}

Hello,

Very fine! I try with success your code.

A question : with python, I’ve a the line # -- coding: utf-8 --
Do Ruby works without problem with utf8 encoding ?
(I work with Ruby 1.8.7 on OS X.)

In my case, input files are in utf8 and I notice output files
are also in utf8 with your code.

Best regards

AM

On 2010-12-29 17:27:02 +0100, Rahul K. said:

I am not sure of your level when you say “real beginner”. Do you mean
beginner in programming or in ruby ?

Perhaps you can start with ruby tutorials. Ruby is not very different
from python. The tutorials (ruby-lang.org) should get you comfortable
with ruby syntax, control flow and other basics.

Hope this helps.

Hello
I know a little more python but I can only build very little script.
Thanks for the suggestion, I need to put myself in total immersion to
learn
the syntax and others basics.

Alain

On 2010-12-29 22:05:23 +0100, w_a_x_man said:

File.makedirs( “new” )

Dir[ “*.tex” ].each{|fname|
text = IO.read( fname )
text2 = HEADER % text
File.open( “new/” + fname, “wb” ){|fh|
fh.print text2
}
}

With ruby 1.9, the only thing to change, is to use fileutils instead
of ftools ?

AM

On 2010-12-29 21:39:07 +0100, Stu said:

Paolo Perrotta
ISBN: 978-1-93435-647-0

For Reference:

The Ruby P.ming Language
Yukihiro M…
ISBN: 0596516177

Thanks for the suggestions

AM

Hello Alain,

Some month ago, To help somebody (N.K.) to switch from its
Tool Command Language, I translated a tcl script (from N.K.)
to Ruby.
The script was aimed to add some LaTeX glue around a Tikz
image, then compile it to produce a pdf file.

The principle is to provide a LaTeX proto with some erb
encapsuled Ruby code, then generate a LaTeX file from the
script options, then compile it to produice (e)pdf,
the (if OK) remove temporary files.

I write several versions from the same tcl model (with the same
API): from the most basic one (for somebody who doesn’t like
the magic Ruby style) to a more generic (i.e. standard) one
which could be use for other similar applications.

http://www.ensta.fr/~diam/ruby/online/tikz2pdf/

The scripts are provide “as is”, just as stuff to start you
one tool!

– Maurice

P.S.1

For those who doesn’t know, Alain is a great contributer
for the Tikz graphic scripting system for TeX:

http://www.texample.net/tikz/examples/author/alain-matthes

P.S.2 (for Alain)

Do you know that Hans Hagen (ConTeXt author) has switch from
Perl to Ruby for it full TeX (ConTeXt) build system some year
ago?

P.S.2 (for all)

Sorry for my bad english!

On 2010-12-30 13:02:31 +0100, mdiam said:

http://www.ensta.fr/~diam/ruby/online/tikz2pdf/

The scripts are provide “as is”, just as stuff to start you
one tool!

– Maurice

Hello maurice,

This is very interesting. I’m not able to understand all the code
actually but it
will be a very insteresting and instructive example

P.S.1

For those who doesn’t know, Alain is a great contributer
for the Tikz graphic scripting system for TeX:

http://www.texample.net/tikz/examples/author/alain-matthes

My question about the script in ruby is to complete my last packages
and documentations with some independent files. It’ to help the readers
with some ready examples. The reader has only to compile the example
and the name of the example corresponding to the section, subsection of
the doc.
Obviously, the packages are about tikz ! (euclidean geometry and
functions)

P.S.2 (for Alain)

Do you know that Hans Hagen (ConTeXt author) has switch from
Perl to Ruby for it full TeX (ConTeXt) build system some year
ago?

No but now I found :
/usr/local/texlive/2010/texmf-dist/scripts/context/ruby/
and it’s very interesting. Thanks!

For Maurice : http://www.ensta.fr/~diam/ruby/ looks very fine and
"c’est en fran

On Thu, Dec 30, 2010 at 9:40 AM, Alain M. [email protected] wrote:

Thanks I bought this book but I only read the first chapters. I like this
book
anf the way use by Chris P. to teach Ruby. (sorry for my english
language)

Chris P. covers object-oriented programming in his book, as well,
and so does the PickAxe.

Mind, you don’t have to use Ruby in an object-oriented fashion,
since Ruby supports procedural programming just as well.

Ok but PickAxe covers 1.9 or I’ve 1.8.7 with OS X. Is this a problem?
I’m afraid to install 1.9 because I work with TextMate and this soft is very
dependent of Ruby.

Well, you could try to find the 2nd Edition of the PickAxe, or upgrade
your Ruby. To do that, you’ll have to ask Google, since I have no idea
what the recommended way would be (I guess rvm or Homebrew).

As far as TextMAte is concerned: I have no idea if it’ll work with
Ruby 1.9, too.


Phillip G.

Though the folk I have met,
(Ah, how soon!) they forget
When I’ve moved on to some other place,
There may be one or two,
When I’ve played and passed through,
Who’ll remember my song or my face.

On Dec 30, 3:54am, Alain M. [email protected] wrote:

%s
}
}

With ruby 1.9, the only thing to change, is to use fileutils instead
of ftools ?

AM

If 1.9 lacks ftools, then require “fileutils” and replace
File.makedirs( “new” )
with
FileUtils.mkdir( “new” )

On Dec 30, 3:02am, Alain M. [email protected] wrote:

Hello,

Very fine! I try with success your code.

A question : with python, I’ve a the line # -- coding: utf-8 --
Do Ruby works without problem with utf8 encoding ?

I don’t know much about utf8.

On Dec 30, 1:02am, Alain M. [email protected] wrote:

A question : with python, I’ve a the line # -- coding: utf-8 --
Do Ruby works without problem with utf8 encoding ?
(I work with Ruby 1.8.7 on OS X.)

Given your ruby version you’ll be fine until you start using
characters with code points > 127

Macintosh:~ $ ruby -e’almoo = “cerveja”; puts almoo’
-e:1: Invalid char \303' in expression -e:1: Invalid char \247’ in expression
-e:1: Invalid char \303' in expression -e:1: Invalid char \247’ in expression
-e:1: warning: parenthesize argument(s) for future version
Macintosh:~ $ ruby -KUe’almoo = “cerveja”; puts almoo
cerveja

Otherwise you’ll have to set $KCODE (-K) to “U” for utf8 mode.