# Platform Independence Programming

**URL:** <https://www.ruby-forum.com/t/platform-independence-programming/97900>\
**Category:** Ruby\
**Created:** [July 11, 2006, 7:06am UTC](https://www.ruby-forum.com/t/platform-independence-programming/97900 "2006-07-11T07:06:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [July 11, 2006, 7:06am UTC](https://www.ruby-forum.com/t/platform-independence-programming/97900/1 "2006-07-11T07:06:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [July 11, 2006, 7:55am UTC](https://www.ruby-forum.com/t/platform-independence-programming/97900/2 "2006-07-11T07:55:36Z")

</div>

On 7/11/06, Minkoo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [July 11, 2006, 8:14am UTC](https://www.ruby-forum.com/t/platform-independence-programming/97900/3 "2006-07-11T08:14:19Z")

</div>

Thanks Patrick.

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

Sincerely,  
Minkoo S.

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [July 11, 2006, 8:23am UTC](https://www.ruby-forum.com/t/platform-independence-programming/97900/4 "2006-07-11T08:23:56Z")

</div>

On 7/11/06, Minkoo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [July 11, 2006, 8:27am UTC](https://www.ruby-forum.com/t/platform-independence-programming/97900/5 "2006-07-11T08:27:05Z")

</div>

On 7/11/06, Minkoo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [July 11, 2006, 8:33am UTC](https://www.ruby-forum.com/t/platform-independence-programming/97900/6 "2006-07-11T08:33:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [July 11, 2006, 8:45am UTC](https://www.ruby-forum.com/t/platform-independence-programming/97900/7 "2006-07-11T08:45:37Z")

</div>

On 7/11/06, Minkoo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [July 11, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/platform-independence-programming/97900/8 "2006-07-11T16:50:12Z")

</div>

“Minkoo S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
