Platform Independence Programming

Hi list.

I’d like to ask a question on platform independent programming with
Ruby.
The very first thing that comes to my mind is “\r\n” (new line) and “”
(directory
separator).

Both of them should be correctly used depending on the OS running Ruby
programs. That being the case, how can I rewrite the following program
in platform independent way?

print “Hello\r\n”
Dir["/tmp/*"]

And I also wonder whether there’s another problem related to platform
independence.

Thanks in advance.

Sincerely,
Minkoo S.

On 7/11/06, Minkoo S. [email protected] wrote:

programs. That being the case, how can I rewrite the following program
in platform independent way?

print “Hello\r\n”

puts “Hello”

Dir[“/tmp/*”]

note your code works fine under windows assuming you have a /tmp

directory
tmp_path = ENV[‘TEMP’] || ENV[‘TMP’] || File.join(‘’,‘tmp’)
Dir[File.join(tmp_path,‘*’)]

And I also wonder whether there’s another problem related to platform
independence.

Plenty :-), fork/process handling, services / daemons, drive
letters/mount points, IPC/pipes, etc. It really depends upon your
needs/goals many things are pretty easy, some are very difficult you
need to be more specific.

Good luck
pth

Thanks Patrick.

I got one more question. The list you’ve shown doesn’t contain
threading.
Is threading platform independent?

Sincerely,
Minkoo S.

On 7/11/06, Minkoo S. [email protected] wrote:

Thanks Patrick.

I got one more question. The list you’ve shown doesn’t contain threading.
Is threading platform independent?

Ruby threads are pretty portable – they are green and not OS, so they
do not have many gotchas. Now OS threads (which are not commonly or
easily used in Ruby programs) are a whole different issue.

pth

On 7/11/06, Minkoo S. [email protected] wrote:

Thanks Patrick.

I got one more question. The list you’ve shown doesn’t contain threading.
Is threading platform independent?

Current versions of Ruby work like what Java people call ‘green’
threads, rather than using native threads; they’ll work even if you’re
running under one of those platforms that doesn’t have support for
threading at all. There is code in the Ruby interpreter that simulates
a multithreaded environment without relying on any platform-specific
threading support.

What about priority of a Thread? Number of priorities vary from OS to
OS,
and it is said that priorities map to different prority levels depending
on
OS even
if threads are green. How is Ruby’s implementaion in this regard?

Sincerely,
Minkoo S.

On 7/11/06, Minkoo S. [email protected] wrote:

What about priority of a Thread? Number of priorities vary from OS to OS,
and it is said that priorities map to different prority levels depending on
OS even
if threads are green. How is Ruby’s implementaion in this regard?

Ruby’s threads have nothing really to do with OS threads – they are
implemented entirely within the interpreter. Note that if you are on
multi-processor box, your app regardless of how many Ruby threads you
are using you will be only using one of them – one process/one
thread. Priorities are also handled within the interpreter. You get
lucky here, not much to worry about.

pth

“Minkoo S.” [email protected] writes:

Hi list.

I’d like to ask a question on platform independent programming with Ruby.
The very first thing that comes to my mind is “\r\n” (new line) and ""
(directory
separator).

Use “\n” and File::SEPARATOR (alternatively, if you care about unix
and windows only (osx counts as unix), “/” is acceptable)

“\n” would map to “\r\n” for text files in windows, and “\r” for text
files in mac os 9. In Unix, it stays as-is.

YS.

print “Hello\r\n”

print “Hello\n”

Dir[“/tmp/*”]

That works in windows & unix. You can also do: Dir[File.join(*%w{/tmp
*})]. (Note the symbol / for root directory or the concept of root
directory is not universal, thus why the “/tmp”).

YS.