General Tips for Smaller Ruby Code

Add a reply

Posted by dfg59 773 days ago:
Can we start a thread with general tips for reducing your code size in Ruby?

Examples:

$><< to output
a,b=readlines to read multiple lines

I'm very new to ruby myself and I find myself totally addicted to this type of game, however I feel that sharing some information about general Ruby tips for producing smaller code could help some of the new guys get rolling on the challenges. I, for one, don't know where to look to find out many of these "tricks" that those coding in the language for some time take for granted.

Thanks,
Drew
Posted by flagitious 772 days ago:
Below are some things I have found to be useful. It's basically a list of things that you should probably not use except for codegolf :) Alot of this I learned from talking with people since I joined, so there are probably lots of tricks like this I don't know yet, hopefully someone can add to this.

?d for numbers like 100, etc
a*i+=1while gets (short while, throwing in = / += at end of statements, no space between while)
using implied $_ in gets, gsub, scan (note gsub = $_.gsub!, no need for ! point)
$* for array initialized to [], but cannot use = operator on it although you can write to it in other ways
$/ for string initialized to "\n"
$$ for a huge number (process number)
? : operator as well as using i==0&&j+=7 instead of j+=7 if i==0
% operator for formatting strings, very useful
a*' ' instead of a.join
1/0 to exit
%w(...) for space separated array of strings
<< to append to arrays/strings
Posted by will 772 days ago:
Don't forget saving a char by using literal newlines, so:

a = "some
data"


instead of:

a = "some\ndata"
Posted by dfg59 772 days ago:
Below are some things I have found to be useful. It's basically a list of things that you should probably not use except for codegolf :) Alot of this I learned from talking with people since I joined, so there are probably lots of tricks like this I don't know yet, hopefully someone can add to this.

?d for numbers like 100, etc
a*i+=1while gets (short while, throwing in = / += at end of statements, no space between while)
using implied $_ in gets, gsub, scan (note gsub = $_.gsub!, no need for ! point)
$* for array initialized to [], but cannot use = operator on it although you can write to it in other ways
$/ for string initialized to "\n"
$$ for a huge number (process number)
? : operator as well as using i==0&&j+=7 instead of j+=7 if i==0
% operator for formatting strings, very useful
a*' ' instead of a.join
1/0 to exit
%w(...) for space separated array of strings
<< to append to arrays/strings

Thanks for the extremely helpful post. As I'm a totally Ruby newbie, can you possible show some examples using your $ variables? Thanks for the help!
Posted by blake8086 694 days ago:
I'm interested in the smallest ways to get input. How does one parse a single line of space-seperated numbers, or multiple lines, into an array?
Posted by carldr 694 days ago:
Try these :

Single line of space-separated numbers into an array:
a=gets.split

Multiple lines into an array:
a=gets(nil).split("\n")

Note that these might not be useful depending on whether you want the newline(s) at the end of the line(s) or not. Also, you can save another couple of bytes on that second one - I'll leave you to work out how :)
Posted by subwindow 653 days ago:
Using "p" for "puts"

Using "getc" instead of "gets" and then iterating over the characters of the string. (your mileage may vary)

"i=j=0" to set initial values of more than one variable

Creative use of =~ (regular expression match)


Anyone know of a "shorter" iterator than using while/end?
Or does anyone know a shorter version of "break"?
Posted by flagitious 642 days ago:
a,b=readlines to read multiple lines

Recently I learned this, you could do this shorter by doing this:

a,b=*$<

or if you want it as an array,

[*$<]
Posted by MyrddinE 6 days ago:
Anyone know of a "shorter" iterator than using while/end?

If you can squeeze the contents of the block into a single statement, you can use this:

statement while expression

Saves 4 or 5 bytes, skipping 'end'.

Add a reply