I just ran into this old post about idioms rewritten in computer (pseudo)code . It occurs to me that I’ve learned a lot more Ruby since then, and I decided to rewrite some of the pseudocode into actual code.
original:
// idiom 1
cop [ 0 ]. goodInPercent = 100 ;
cop [ 1 ]. goodInPercent = 0 ;
my version:
class Interrogation
@Interrogators = []
def GoodCopBadCop
@Interrogators = [ cops . select { | cop | cop . is_good? }. first , cops . select { | cop | cop . is_bad? }. first ]
cops . delete ( cops . select { | cop | cop . is_good? }. first )
end
end
original:
// idiom 2
isCrowd = personCounter >= 3 ;
my version:
class Fixnum
def is_crowd?
self > 2
end
end
3 . is_crowd?
original:
// idiom 3
injury += insult ;
my version:
return ( insult + injury )
original:
// idiom 4
1 : board . draw ();
goto 1 ;
my version:
session [ :return_to ] ||= 'http://http://mahiwaga.net/drawing-board'
redirect_to session . delete ( :return_to )
original:
// idiom 5
if ( bird [ 1 ]. feather == bird [ 2 ]. feather ) {
bird [ 1 ]. flock ( bird [ 2 ]);
}
my version:
flock . collect { | bird | feathers . each { | feather | bird . find_by_feather ( feather )}}
original:
// idiom 6
a = getThickness ( 'blood' );
b = getThickness ( 'water' );
assert ( a > b );
class Liquid
attr_accessor :spec_grav
def initialize args
args . each do | k , v |
instance_variable_set ( "@ #{ k } " , v ) unless v . nil?
end
end
def is_thicker_than? ( other_liquid )
self . spec_grav > other_liquid . spec_grav
end
end
blood = Liquid . new ( :spec_grav => 1 . 0506 ) # reference: http://www.clinchem.org/cgi/content/abstract/20/5/615
water = Liquid . new ( :spec_grav => 1 . 0000 )
blood . is_thicker_than? ( water )
(see: Specific Gravity of Blood and Plasma at 4 and 37°C )
original:
// idiom 7
a_spade_a_spade ();
my version:
class Spade
def call ( value )
@name = value
end
end
spade = Spade . new
spade . call ( "a spade" )
original:
// idiom 8
die ( 1000 );
function die ( max ) {
for ( i = 1 ; i <= max ; i ++ ) {
cut ();
}
}
my version:
Death . find_by_cut ( '1000' )
original:
// idiom 9
prey = 'worm' ;
time = getCurrentTime ();
if ( time >= 4 && time <= 8 ) {
bird . catch ( prey );
}
my version:
bird = Creature . new ( :shift => 'early' )
worm = Creature . new
if bird . shift == 'early'
bird . catch ( worm )
end
original:
// idiom 10
while ( rome . fire () ) {
doFiddle ();
}
my version:
Nero ( :instrument => 'fiddle' ) while Rome . is_burning?
original:
// idiom 11
function getValue ( garbage ) {
return garbage ;
}
my version:
Value . In ( :Garbage )
print Value . Out
==> Garbage
original:
// idiom 12
take ( salt * . 01 );
my version:
Salt . take ( :mass => 0 . 0647 9891 , :unit => 'gram' )
original:
// idiom 13
var here = false ;
var there = false ;
my version:
! ( here or there )
original:
// idiom 14
if ( i == 2 ) {
tango ();
}
my version:
def self . takes ( value )
if value == 2 then self . tango ;
end
original:
// idiom 15
days = 365 ;
for ( day = 1 ; day <= days ; day ++ ) {
if ( random ( 0 , 100 ) <= 50 ) apple ++ ;
}
if ( apple <= days ) doctor ();
my version:
year . collect { | day | if day . apples . count > 0 then doctor . keep_away }
original:
// idiom 16
if ( ! dogs . sleep () ) {
disturb ( dogs );
}
my version:
if dog . is_asleep?
dog . lay
end
original:
// idiom 17
function tunnel () {
var dark ;
for ( i = 0 ; i < 10 ; i ++ ) {
dark = true ;
}
dark = ! dark ;
return dark ;
}
my version:
Class Tunnel
def self . is_there_light?
return ( self . position == 'END' )
end
end
original:
// idiom 18
if ( ape . inLineOfSight ( it . x , it . y ) ) ape . do ( it );
my version:
if monkey . does_see? ( :actor )
monkey . do ( Monkey . action_seen ( :actor ))
end
original:
// idiom 19
return way . my || way . high ;
my version:
return way ( :my ) || way ( :high )
original:
// idiom 20
hay [ random ( 0 , hay . length - 1 ) ] = 'needle' ;
my version:
hay = Stack . new
hay . find ( 'needle' )
original:
// idiom 21
a = 0 ;
b = 1 ;
hand [ a ]. wash ( hand [ b ]);
hand [ b ]. wash ( hand [ a ]);
my version:
hand [ 'left' ]. wash ( hand [ 'right' ])
hand [ 'right' ]. wash ( hand [ 'left' ])
original:
// idiom 22
function getGain ( pain ) {
return pain >= 1 ;
}
my version:
if ( pain . level == 0 ) then gain . level = 0
original:
// idiom 23
if ( cooks >= 3 ) {
broth = null ;
}
my version:
class Broth
def is_spoiled?
self . cook . count >= TOO_MANY
end
end
original:
// idiom 24
if ( a != 'cake' ) a . eat ();
my version:
Class Cake
def Cake . have ()
Cake . is_had? = Cake . is_eaten? ? false : true
end
def Cake . eat ()
Cake . is_eaten? = Cake . is_had? ? false : true
end
end
original:
// idiom 25
doesStand = you == me ;
my version:
if We . is_united? then We . stand ()
if We . is_divided? then We . fall ()
original:
// idiom 26
var location = getLocation ();
if ( location == 'rome' ) {
do ( location . getCitizen () );
}
my version:
if person . locale == 'Rome'
person . behavior ( :Roman )
end
posted by
mahiwaga