Pascal vs. Ruby

29 06 2009

On ruby-talk somebody was asking for a method which will be used to generate a random string, by returning either a lowercase letter or one of the digits 0-9 when called. As an example he posted the following Pascal program:

function GetRandomChar: char;
var
 r: integer;
begin
 r := random(36);
 case r of
   0..25: result := chr(ord('a') + r);
   else : result := chr(ord('0') + r);
 end;
end;

“Translating” this to Ruby, this is what I got:

In your face, Niklaus Wirth! ;-)

P.S. Actually I do have fond memories of the good old Turbo Pascal days.



Trackbacks


No Trackbacks

Comments

Display comments as (Linear | Threaded)
15 08 2009
#1 Blestan Tabakov (Reply)

try this:

var allowed_chars:string=‘abcdefghijklmnopqrstuvwxyz0123456789’ ;
// add as many chars as you want

function generate_string(len: integer): string;inline;
begin while len>0 do begin result:=result+random(length(allowed_chars); dec(len); end
end;

no "if" no "else" ... can add any chars to the sting ….fast .. clear code
pascal rules!


Add Comment


Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Textile-formatting allowed

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA 1CAPTCHA 2CAPTCHA 3CAPTCHA 4CAPTCHA 5


Standard emoticons like :-) and ;-) are converted to images.