Skip to content

Instantly share code, notes, and snippets.

@glejeune
Created November 23, 2011 19:28
Show Gist options
  • Select an option

  • Save glejeune/1389640 to your computer and use it in GitHub Desktop.

Select an option

Save glejeune/1389640 to your computer and use it in GitHub Desktop.

Show me the code

[[7,"Qix"],[5,"Bar"],[3,"Foo"]].inject(_.to_s){|r,d|i,k=d;r[/^/]=(_%i==0)?k:"";r.gsub!(/#{i}/,k);(r[/\D/].nil?)?r:r.tr("0124689","")}

This is Ruby \o/

No! Wait, this one is better :

((f={3=>:Foo,5=>:Bar,7=>:Qix}).sort.collect{|i,j|_%i==0?j:""}+_.to_s.split(//).collect{|c|f[c.to_i]||""}).join.sub(/^$/,_.to_s) #foobarqix

OK, but your code must run in a JVM

No problem, just use JRuby

Why the code is so ugly ?

To be able to tweet it ;)

How do I test that?

Use IRB :

$ jirb                                                                                                                                                     
jruby-1.6.5 :001 > [[7,"Qix"],[5,"Bar"],[3,"Foo"]].inject(_.to_s){|r,d|i,k=d;r[/^/]=(_%i==0)?k:"";r.gsub!(/#{i}/,k);(r[/\D/].nil?)?r:r.tr("0124689","")}
 => "Foo" 
jruby-1.6.5 :002 > 51
 => 51 
jruby-1.6.5 :003 > [[7,"Qix"],[5,"Bar"],[3,"Foo"]].inject(_.to_s){|r,d|i,k=d;r[/^/]=(_%i==0)?k:"";r.gsub!(/#{i}/,k);(r[/\D/].nil?)?r:r.tr("0124689","")}
 => "FooBar" 
jruby-1.6.5 :004 > 53
 => 53 
jruby-1.6.5 :005 > [[7,"Qix"],[5,"Bar"],[3,"Foo"]].inject(_.to_s){|r,d|i,k=d;r[/^/]=(_%i==0)?k:"";r.gsub!(/#{i}/,k);(r[/\D/].nil?)?r:r.tr("0124689","")}
 => "BarFoo" 
jruby-1.6.5 :006 > 21
 => 21 
jruby-1.6.5 :007 > [[7,"Qix"],[5,"Bar"],[3,"Foo"]].inject(_.to_s){|r,d|i,k=d;r[/^/]=(_%i==0)?k:"";r.gsub!(/#{i}/,k);(r[/\D/].nil?)?r:r.tr("0124689","")}
 => "FooQix" 
jruby-1.6.5 :008 > 13
 => 13 
jruby-1.6.5 :009 > [[7,"Qix"],[5,"Bar"],[3,"Foo"]].inject(_.to_s){|r,d|i,k=d;r[/^/]=(_%i==0)?k:"";r.gsub!(/#{i}/,k);(r[/\D/].nil?)?r:r.tr("0124689","")}
 => "Foo" 
jruby-1.6.5 :010 > 15
 => 15 
jruby-1.6.5 :011 > [[7,"Qix"],[5,"Bar"],[3,"Foo"]].inject(_.to_s){|r,d|i,k=d;r[/^/]=(_%i==0)?k:"";r.gsub!(/#{i}/,k);(r[/\D/].nil?)?r:r.tr("0124689","")}
 => "FooBarBar" 
jruby-1.6.5 :012 > 33
 => 33 
jruby-1.6.5 :013 > [[7,"Qix"],[5,"Bar"],[3,"Foo"]].inject(_.to_s){|r,d|i,k=d;r[/^/]=(_%i==0)?k:"";r.gsub!(/#{i}/,k);(r[/\D/].nil?)?r:r.tr("0124689","")}
 => "FooFooFoo" 
jruby-1.6.5 :014 >

I don't want to use IRB!

OK, no problem, just run the following line in your terminal :

jruby < <(curl -s https://raw.github.com/gist/1389640/a91749acc1a1739cdd5cb541cd3d35b8d4aad825/run.rb)
((f={3=>:Foo,5=>:Bar,7=>:Qix}).sort.collect{|i,j|_%i==0?j:""}+_.to_s.split(//).collect{|c|f[c.to_i]||""}).join.sub(/^$/,_.to_s) #foobarqix
[[7,"Qix"],[5,"Bar"],[3,"Foo"]].inject(_.to_s){|r,d|i,k=d;r[/^/]=(_%i==0)?k:"";r.gsub!(/#{i}/,k);(r[/\D/].nil?)?r:r.tr("0124689","")}
require 'net/https'
http = Net::HTTP.new('raw.github.com', 443)
http.use_ssl = true
_,fbq=http.get('/gist/1389640/08b43fe2d439979250d81f7559139d6943a8b7c0/main-v2.rb',nil)
(1..100).each { |_|
puts eval fbq
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment