# Write Fortran in Ruby

**URL:** <https://www.ruby-forum.com/t/write-fortran-in-ruby/75686>\
**Category:** Ruby\
**Created:** [March 2, 2006, 12:48pm UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686 "2006-03-02T12:48:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [March 2, 2006, 12:48pm UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686/1 "2006-03-02T12:48:44Z")

</div>

A fun, but ultimately pointless exercise from the “Python  
for Fortran Programmers” thread in news:comp.lang.fortran:

Try to write the following Fortran 77 program (note the  
6 space indentation) in Ruby so that it looks as close  
to the Fortran as possible without extending Ruby:

```
   program ii
   integer i
   do i=0,10
      print*,i
      if (i.gt.5) goto 1
   enddo

```

1 print\*,i\*i  
end

Here’s the Python candidate to date:

for i in range(10):  
#begin  
print i  
if i\>5: break  
#end  
print i\*i

May an alternative solution which extends Ruby with  
a mini Fortran DSL would also be interesting?

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [March 2, 2006, 3:14pm UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686/2 "2006-03-02T15:14:21Z")

</div>

Bil K. wrote:

> 1 print\*,i\*i  
> end

My Fortran’s a little rusty (I never learnt it); does this program do  
the  
right thing?

```
 #program ii
 #integer i
  for i in 0..10
     print *i
     if (i.>5) then break end
  end#do

```

1; print _i_i  
#end

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [March 2, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686/3 "2006-03-02T15:48:47Z")

</div>

Bil K. wrote:

> 1 print\*,i\*i  
> end

Sorry, output should be:

0  
1  
2  
3  
4  
5  
6  
36

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [March 2, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686/4 "2006-03-02T16:13:49Z")

</div>

I said earlier:

> ```
> #program ii
> #integer i
> for i in 0..10
> print *i
> if (i.>5) then break end
> end#do
> 
> ```
> 
> 1; print _i_i  
> #end

A bad guess. Swap the prints for putses like this:  
puts \*i  
…  
1; puts _i_i

Any ideas for making “then break end” look more like “goto 1”?

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [March 2, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686/5 "2006-03-02T17:03:43Z")

</div>

Your Fortran program is not legal since the value of i outside the loop  
is not defined. Over the years, I have seen are 6, 7 and runtime error  
(although you are not likely to see the latter with slack modern  
workstation compilers) as a value.

I would suggest putting the print of i\*i inside a block if.

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [March 2, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686/6 "2006-03-02T16:30:13Z")

</div>

On Fri, 3 Mar 2006, Dave B. wrote:

> A bad guess. Swap the prints for putses like this:  
> puts \*i  
> …  
> 1; puts _i_i
> 
> Any ideas for making “then break end” look more like “goto 1”?
> 
> Cheers,  
> Dave

```
 harp:~ > cat a.f
       program ii
       integer i
       do i=0,10
          print*,i
          if (i.gt.5) goto 1
       enddo
 1 print*,i*i
       end

 harp:~ > f77 a.f && a.out
  0
  1
  2
  3
  4
  5
  6
  36

 harp:~ > cat a.rb

 i = catch('1') do
   (0..10).each do |i|
     puts i
     throw '1', i if i > 5
   end
 end
 puts i * i

 harp:~ > ruby a.rb
 0
 1
 2
 3
 4
 5
 6
 36

```

-a

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [March 2, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686/7 "2006-03-02T18:08:48Z")

</div>

In an attempt to make it look at close to the fortran and still run  
(even if silly):

```
   :program; i = 0
   Integer i
   i.upto(10) do |i|
   print i,$/
       if (i > 5) : break end
   end
   print i*i,$/
   exit
```

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [March 2, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686/8 "2006-03-02T20:08:52Z")

</div>

I guess using a range is slightly better. I know, enough already. I  
just wanted it to match the same number of lines (8) and look as close  
as I could.

```
   :program; i = 0
   Integer i
   (0..10).each do |i|
       print i,$/
       if (i > 5) : break end
   end
   print i*i,$/
   exit
```

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [March 3, 2006, 1:06am UTC](https://www.ruby-forum.com/t/write-fortran-in-ruby/75686/9 "2006-03-03T01:06:24Z")

</div>

Hi!

At Thu, 2 Mar 2006 20:48:37 +0900,Bil K. wrote:

> ```
> enddo
> 
> ```
> 
> 1 print\*,i\*i  
> end

First of all there is no programming language calles “Fortran 77”. The  
correct name is “FORTRAN 77”. The spelling “Fortran” is used for more  
recent versions like “Fortran 90”. Next the above program is no valid  
FORTRAN 77. If a compiler sticks to the FORTRAN 77 specification it  
will issue a syntax error because “enddo” is unknown to it. FORTRAN 77  
do-loops start with the DO keyword followed by a label. The label  
names the line that marks the end of the loop. The labelled statement  
_must_ be an executable one. People tend to use “continue” for this  
purpose.

```
   program ii
   integer i
   do 1 i=0,10
      print*,i
      if (i.gt.5) goto 2

```

1 continue  
2 print\*,i\*i  
end

If your compiler supports “end do” (which is no F77) it may as well  
support “exit” (which isn’t either) so that you can write:

```
   program ii
   integer i
   do i=0,10
      print*,i
      if (i.gt.5) exit
   end do
   print*,i*i
   end

```

Besides that all variables starting with an ‘i’ are already integers  
so that you can write

```
   program ii
   do i=0,10
      print*,i
      if (i.gt.5) exit
   end do
   print*,i*i
   end

```

Nevertheless I prefer Fortran 90+ and write

program ii  
do i=0,10  
print\*,i  
if (i\>5) exit  
end do  
print\*,i\*i  
end

You may need to provide the command line option “-free” or similar :-\>

I learned FORTRAN 77 on an IBM Mainframe using a terminal that had a  
“Mixed case display/Uppercase only display” switch 🙂

Josef ‘Jupp’ Schugt
