Skip to main content
17 votes

Tips for golfing in Ruby

Use operator methods instead of parentheses Let's say you want to express a*(b+c). Because of precedence, a*b+c won't work (...
Cyoce's user avatar
  • 3,341
15 votes

Tips for golfing in Ruby

New features in Ruby 2.3 and 2.4 It's good to stay abreast of new language features that will help your golf game. There are a few great ones in the latest Rubies. Ruby 2.3 The safe navigation ...
Jordan's user avatar
  • 10.1k
14 votes

Tips for golfing in Ruby

New features in Ruby 2.7 (experimental) Ruby 2.7 is in prerelease (as of 17 Jun 2019) and has some features that look great for golfing. Note that some of them might not make it into the final 2.7 ...
Jordan's user avatar
  • 10.1k
11 votes

Tips for golfing in Ruby

Save some bytes when removing repeated elements of an array a.uniq # before a|[] # after ^^ If you will be using an empty array ...
Cyoce's user avatar
  • 3,341
8 votes

Tips for golfing in Ruby

Save a byte when printing a word with symbols This is a bit situational, but every byte counts! puts"thing" # before puts:thing # after ^
Cyoce's user avatar
  • 3,341
8 votes

Tips for golfing in Ruby

To join an array, instead of this [...].join do this [...]*'' which saves 2 bytes. To join with a separator use ...
Markus's user avatar
  • 131
8 votes

Tips for golfing in Ruby

Avoid Array#repeated_permutation and Array#repeated_combination Credit to @AsoneTuhid who golfed the code for repeated ...
Dingus's user avatar
  • 11.2k
7 votes

Advice for ruby

Use ARGV ($*) and mass assignment (disclaimer: I don't know Ruby, but this works on TIO) p,a,b=$* p=p.to_i b=b.to_i 28 26 bytes instead of 30 (thanks to Snack ...
AdmBorkBork's user avatar
  • 43.5k
7 votes

Tips for golfing in Ruby

On looping while...end If you need to break out of the loop, while condition;code;end will ...
Asone Tuhid's user avatar
  • 2,363
6 votes

Tips for golfing in Ruby

Re(ab)use predefined globals There is a whole bunch of predefined global variables that you can use instead of initialising new variables. A very common example is that you have some golfed one-...
daniero's user avatar
  • 18.6k
6 votes

Advice for ruby

If you need a full program with stdin/stdout io for some reason, the shortest you can do is use the -n flag to shorten one call to ...
Pavel's user avatar
  • 9,377
6 votes

Advice for ruby

Use a lambda Answers are typically allowed as lambda functions with your input/output being the parameters/return value of the lambda, so you can do this: ...
Snack's user avatar
  • 351
6 votes
Accepted

Generating versions of an array with elements changed in ruby

Ruby, 102 99 98 95 bytes (k=input.map &:dup)[$.-=1][0]=%i[nop jmp acc][k[$.][0]<=>:jmp]until(r=try_with k||$:)[1]>:t;p r Try it online! I assume that ...
Dingus's user avatar
  • 11.2k
5 votes

Tips for golfing in Ruby

Avoid String#sub! Instead of a.sub!b,c save a byte with a[b]&&=c Both return ...
Dingus's user avatar
  • 11.2k
5 votes

Tips for golfing in Ruby

Array#assoc/rassoc When you have an array of arrays and want to find the sub-array that starts with a particular value, don't ...
Jordan's user avatar
  • 10.1k
5 votes

Tips for golfing in Ruby

To subtract one (-1), instead of doing this (i - 1) * 2 do the following ~-i * 2 which will save you 2 bytes in cases ...
Markus's user avatar
  • 131
5 votes

Tips for golfing in Ruby

Use eval instead of reduce While array.sum is great and shorter than ...
daniero's user avatar
  • 18.6k
5 votes

Tips for golfing in Ruby

Fun with arrays There are many ways to do the same thing in ruby, and often times the default methods are inefficient - as a case study, a new arrays with set size and initialized to all ...
Unihedron's user avatar
  • 1,295
5 votes

Tips for golfing in Ruby

New features in Ruby 2.5 and 2.6 Ruby keeps getting new features that look like they could be handy for golfing. Here are some that caught my eye. If there's any you think should be added feel free ...
Jordan's user avatar
  • 10.1k
3 votes

Tips for golfing in Ruby

Use ~// and !// to match against $_ (last line read) These can be used with the ...
Asone Tuhid's user avatar
  • 2,363
3 votes

Tips for golfing in Ruby

Cloning strings Strings are mutable in Ruby so if you have two variables holding the same string (a=b="string") and you modify one (...
Asone Tuhid's user avatar
  • 2,363
3 votes

Tips for golfing in Ruby

Filtering a specific class from an Array Suppose, you've to choose only Integer from ...
vrintle's user avatar
  • 2,990
3 votes

Golf my iteration function

Ruby, 94 96 bytes Using loops to do concatenations that followed such a simple pattern felt cumbersome, so I utilized the creative use of join instead, further ...
Value Ink's user avatar
  • 12.6k
2 votes
Accepted

Golf my iteration function

Ruby, 87 bytes I=->k{eval ("->f%d{"*-~k+"f%d.times{f1=f0[f1]};f1"+"[f%d]"*~-k+?}*-~k)%[*0..k,k,*2..k]} Try it online!
G B's user avatar
  • 22.2k
2 votes
Accepted

How to improve Ruby code under some restrictions

100 bytes For my purposes, this is good enough. h=[],n=?~.ord,n f=->a{b,c,d=a;b ?a==b ?~-a:a==a-[c-c]?[[b,c,f[d]],~-c,b]:b:p(n+=n)} h=f[h]until h==n ...
Simply Beautiful Art's user avatar
2 votes

How to improve Ruby code under some restrictions

105 You can save three bytes by declaring n as a local variable (instead of a global) before f: ...
Jordan's user avatar
  • 10.1k
2 votes

Crash IRB (interactive Ruby)

8 characters Similar to chinese perl goth's answer: $>.close $> is an alias for STDOUT.
Jordan's user avatar
  • 10.1k
2 votes

Tips for golfing in Ruby

To generate symmetric output, you can use a single range like (-x..x) and then map a function on the absolute value: (-8..8).map{|x|x.abs} Use short-circuit ...
G B's user avatar
  • 22.2k
2 votes

Tips for golfing in Ruby

Is it an array? Originally from histocrat. Rather than writing a.class==Array # 14 bytes you could write ...
Simply Beautiful Art's user avatar
2 votes

Tips for golfing in Ruby

String split function uses $; or space by default; if input is space-seperated, you can omit the argument. ...
Unihedron's user avatar
  • 1,295

Only top scored, non community-wiki answers of a minimum length are eligible