Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby interview question

Tags:

ruby

perl

I got this question in a previous interview and couldnt do it , any idea?

  1. What does this do:

    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=( 
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++; 
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++ 
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=` 
    
like image 921
fenec Avatar asked Jul 16 '10 21:07

fenec


People also ask

Why use Ruby?

Although Ruby is probably most famous for its use in web development, it has many other uses, too. Some of these include automation, command-line tools, static site generation, DevOps, web scraping, and data processing. Perhaps most importantly, Ruby is a highly versatile and portable language.

Where can I practice Ruby?

Ruby (Exercism) And the best place to practice Ruby programming is on Exercism. Exercism is an free online coding platform where you can practice your Ruby coding skills through well-designed programming exercises.


1 Answers

This is Perl code that prints out "Just another Perl hacker."

While most of the $_, $=, etc. variables are available in Ruby as well, the presence of statements such as $,++ indicate Perl, which actually has pre- and post-increment operators, unlike Ruby.


I went in with Vim and replaced all the symbols with their English equivalent. I munged something up since the output is now "Just another Per hacker" (missing the L on Perl), but here's what I came up with:

use English;
`$FORMAT_LINES_PER_PAGE`;
$ARG=\%!;($ARG)=/(.)/;$FORMAT_LINES_PER_PAGE=++$OUTPUT_AUTOFLUSH;
($INPUT_LINE_NUMBER,$/,$OUTPUT_FIELD_SEPARATOR,$OUTPUT_RECORD_SEPARATOR,$LIST_SEPARATOR,$SUBSCRIPT_SEPARATOR,$FORMAT_TOP_NAME,$OFMT,$FORMAT_NAME,$MULTILINE_MATCHING,$FORMAT_LINE_BREAK_CHARACTERS,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$LIST_SEPARATOR),$FORMAT_LINES_PER_PAGE++;
$INPUT_LINE_NUMBER++;
$INPUT_LINE_NUMBER++; $ARG++;$ARG++;
($ARG,$OUTPUT_RECORD_SEPARATOR,$OUTPUT_FIELD_SEPARATOR)=($FORMAT_NAME.$LIST_SEPARATOR."$SUBSCRIPT_SEPARATOR$/$FORMAT_PAGE_NUMBER[$CHILD_ERROR]$ARG$OUTPUT_RECORD_SEPARATOR$OUTPUT_FIELD_SEPARATOR$FORMAT_LINE_BREAK_CHARACTERS$FORMAT_PAGE_NUMBER[$CHILD_ERROR]",$LIST_SEPARATOR&$FORMAT_NAME,$OFMT,);
$OUTPUT_FIELD_SEPARATOR++ ;
$OUTPUT_FIELD_SEPARATOR++;
$FORMAT_TOP_NAME|=$LIST_SEPARATOR;
`$ARG$OUTPUT_RECORD_SEPARATOR$OUTPUT_FIELD_SEPARATOR$/$FORMAT_LINE_BREAK_CHARACTERS$SUBSCRIPT_SEPARATOR$FORMAT_NAME$MULTILINE_MATCHING$FORMAT_PAGE_NUMBER[$CHILD_ERROR]$INPUT_LINE_NUMBER$FORMAT_NAME$MULTILINE_MATCHING${#}$FORMAT_PAGE_NUMBER[$CHILD_ERROR]$SUBSCRIPT_SEPARATOR$OUTPUT_RECORD_SEPARATOR$LIST_SEPARATOR$FORMAT_TOP_NAME$FORMAT_NAME$MULTILINE_MATCHING.>&$FORMAT_LINES_PER_PAGE`
like image 100
Mark Rushakoff Avatar answered Oct 21 '22 18:10

Mark Rushakoff