This is a translation of Radarek's excellent article.

You can write FORTRAN in any language

A language that doesn’t affect the way you think about programming is not worth knowing

You can write C code in Ruby:

Car = Struct.new(:model, :color, :opt)

CAR_MODEL_BWM       = 0
CAR_MODEL_AUDI      = 1

CAR_OPT_GAS         = 0x01
CAR_OPT_AIR_COND    = 0x02
CAR_OPT_ROOF_RACK   = 0x04

CAR_COLOR_BLACK     = 0
CAR_COLOR_RED       = 1
CAR_COLOR_GREEN     = 3
CAR_COLOR_BLUE      = 4
CAR_COLOR_WHITE     = 5

CAR_MODEL_NAMES = [
  "BMW",
  "AUDI"
]

CAR_COLOR_NAMES = [
  "black",
  "red",
  "green",
  "blue",
  "white"
]

def car_create(model, color, opt = 0)
  car = Car.new
  car.model = model
  car.color = color
  car.opt   = opt
  return car
end

def car_print(car)
  printf("[Car] model: %s, color: %s\n", CAR_MODEL_NAMES[car.model], CAR_COLOR_NAMES[car.color])
  printf("Options: \n")
  if car_has_gas(car)
    printf(" - gas\n")
  end
  if car_has_air_cond(car)
    printf(" - air condition\n")
  end
  if car_has_roof_rack(car)
    printf(" - roof rack\n")
  end
end

def car_max_speed(car)
  case car.model
  when CAR_MODEL_AUDI
    return 200
  when CAR_MODEL_BWM
    return 220
  else
    return -1
  end
end

def car_has_gas(car)
  return (car.opt & CAR_OPT_GAS) != 0
end

def car_has_air_cond(car)
  return (car.opt & CAR_OPT_AIR_COND) != 0
end

def car_has_roof_rack(car)
  return (car.opt & CAR_OPT_ROOF_RACK) != 0
end

car = car_create(CAR_MODEL_BWM, CAR_COLOR_BLACK, CAR_OPT_AIR_COND)
car_print(car

You can write Java code in Ruby:

class User
  def initialize(name, age)
    self.setName(name);
    self.setAge(age);
  end
  
  def setName(name)
    @name = name;
  end
  
  def getName()
    return @name;
  end
  
  def setAge(age)
    @age = age;
  end
  
  def getAge()
    return @age;
  end
end

class Iterator
  def initialize(array)
    @index = 0;
    @array = array
  end
  
  def hasNext()
    return @index < @array.length();
  end
  
  def getNext()
    @index += 1;
    return @array[@index - 1];
  end
end

array = [User.new("john", 20), User.new("ala", 30), User.new("dave", 15)];
it = Iterator.new(array);
while (it.hasNext())
  user = it.getNext();
  puts("Name: " + user.getName() + ", Age: " + user.getAge().to_s());
end

You can write Perl code in Ruby:

while gets
  chomp
  next if /^#/ or /^!/
  break if /quit/
  
  if /(\d+)/
    print $1
  end
end

The purpose of this note: if you want to program in Ruby, learn the language properly, not just the syntax with standard library. Learn it long enough (or effective enough) until you learn it's style, mindset, idioms and nuances. Then write your Ruby programs using these idioms, standards, way of naming and structing and so on. Don't stop until you learn almost all about it - and when you do, you'll find a lot more to be learned about. You probably already know what OOP is, so put emphasis on non-obvious basics, like method-calling, mixins, includes, extends, metaclasses and so on. Experience life without static typing, experience the power of Ruby's metaprogramming possibilities (and dangers).

Otherwise there'll be a chance you're not writing Ruby, but some other language using simply Ruby syntax. If so - why pretend to write Ruby?

1 Response to “You can write FORTRAN in any language”

  1. Matt Platte Says:

    Ha! Good one! I expected to find a FORTRAN interpreter done in Ruby.

    Sometimes I’m nostalgic for my first computer language, but not nostalgic enough that I actually want to use it!

Sorry, comments are closed for this article.