Superators Add New Operators to Ruby 27
This one’s for the fellow DSL mavens. I’m releasing now a new library I’ve developed that adds new operators to Ruby. Install the superators gem and run the following code in irb:
require 'rubygems'
require 'superators'
class Array
superator "<---" do |operand|
self << operand.reverse
end
end
["jay"] <--- "spillihp"Dear demagogues of domain specificity! Yes, this actually works. Now, how about this cutie:
require 'rubygems'
require 'superators'
class String
superator "++" do |operand|
upcase + operand.upcase
end
superator "-~+~-" do |operand|
puts "Using pretty superators"
self + operand
end
end
p "Tangy" ++ "Erlangy"
p "Super" -~+~- "ators"Superators are a superset of Ruby operators. And they’re pretty super too.

To elaborate more, they work by exploiting the way Ruby parses binary and unary operators. For example, the code
foo ++- baris equivalent to
foo.+(bar.-@().+@())and the Superators library keeps track of the unary operators called on the operand in their appropriate order. A valid Superator’s format is simple: take (almost) any Ruby binary operator and tail on an indefinite number of unary operators after it.
These are the Ruby binary operators you can use: ** * / % + - << >> & | ^ <=> >= <= < > === == =~And here are the unary operators: - ~ +
The only operators you can’t use are the logical ones (e.g. &&, ||, !, and, or, not, etc.) Ruby offers no way of overloading these.
I’m releasing this into the wild to hopefully see what mischief it stirs. But take heed! Superators are second-class citizens still and are victims of vicious discrimination in certain circumstances. In the current implementation the second operand is extend()ed with a Ruby module (unobtrusively setting a flag for later inspection), therefore a Superator can only have a second operand capable of possessing its own eigenclass. Superators will not work with second operands such as true, false, nil, Symbols or Fixnums yet. I’m working on a branch that could fix this if my mad scientist lab experiments bear fruit (or a resentful monster whom I chase to the Arctic Circle after he destroys those I love). Stay tuned.
Despite this limitation, Superators can still be used effectively! Use it when you expect the second operand to be an Array, String, Hash, Range, Regexp, Class, or whatever.
I should note that I strongly frown upon any use of Superators outside of the DSL space. Don’t use it in your company’s Rails app controllers. Use it in your tenderly crafted Ruby DSLs where they make sense.
Enjoy, comrades.
Update: There seems to be some confusion that Superators are a C extension that modify Ruby’s internals. A valid Superator is actually just valid Ruby—something we’ve had at our disposal this whole time!
Trackbacks
Use the following link to trackback from your own site:
http://jicksta.com/articles/trackback/39
-
dickgirl
-
shemale midget
-
Hedgehog XXX the Sonic
-
the hentai incredibles
-
As Fitness Models
-
Nude Portillo Veronica
-
escorts atlanta
-
videos amateur adult
-
final 7 fantasy porn
-
free paris hilton porn
-
hard hentai core
-
sassy ass
-
kissing the rain in
-
14yo nude
-
fuck father daughter
-
Son Sucks Dad
-
Topless models
-
nakid
-
costa rican hookers
-
Techniques on Eating a Girl Out
-
dry sex
-
Stoned Girls
-
Nipple SLIP Galleries
-
lisa bonet nude
-
nirvana smells like teen spirit
-
Vaginas Realistic
-
scary dolls
-
variations penthouse
-
Sex Nurse Having
-
Next Sexy Door Amateur Girls
-
Blond Pussy Young
-
Paris Hilton in the bath! Paris Hilton sex tape video
-
g y boy
-
teen suqirt sex
-
nude blanchett cate
-
sex beacg
-
hentia titians teen
-
college fcuk fest videos
-
virgin camel toe
-
melissachristine flashes
-
catoons porno
-
teen titans raven fuck
-
entai simpsoms
-
freevintageporn
-
sex /fagina boobs
-
sper in pussy
-
boobdd
-
mucle underwear
-
brymova marketa porno
-
female celebritiess sex
-
boobs mautre
-
pretnant girls sex
-
undearge girls
-
naked pictures womem
-
swalllow that cum
-
marge simpson having sex
-
scooby doo xxx pics
-
vilageladies
-
free wifeysworld
-
with niples puffy teens
-
free spankinge
-
myffirstsexteacher
-
porncartoon shemale
-
dolls scary
-
women lhotos old sexy
-
emmaswildworld samples
-
videos sex anall
-
thumbs teen emily
-
seso de juegos
-
sperm shooting out of the penis
-
cameron diaz on sybian
-
managenurseradio.js.cn
-
riskviolentstomach.jl.cn
-
spitenoun.gd.cn
-
jointdrawer.hi.cn
-
toybathlend.xz.cn
-
bottomcompetedot.nx.cn
-
sheepboast.ln.cn
-
nailsacred.ac.cn
-
fryrelieve.bj.cn
-
foldpupil.bj.cn
-
rescuehesitateartificial.jl.cn
-
jointdrawer.hi.cn
-
hunttrunktough.xj.cn
-
fryrelieve.bj.cn
-
hunttrunktough.xj.cn
-
rescuehesitateartificial.jl.cn
-
toybathlend.xz.cn
-
ticketleanamongst.hb.cn
-
governflameswallow.gd.cn
-
Recently leaked footage of the new Paris Hilton sex tape
-
Where can i download paris hilton sex tape?
-
jimsotonna dropped by
-
Erotic Pregnant Teen Sex Stories
-
Malayalam Sex Stores
-
I Just Want To Make Love To You
-
Latin Eye Candy 9
-
W W W Hot Spise Xxx Sex
-
Pamela David Topless
-
Fantasy Wall
-
Free Nude Celebrities Monica Keena
-
Cfnm Nude Setups
-
The Horses Sex
-
Wild Hornny Sex Acts
-
Penus Position
-
Hi There. I'm blog hopping.
-
Mature In Bra And Garterbelt
-
Nude Sex Plya Free Movie
-
Cheating Interracial Wives Stories
-
Kristen Holt Nude Pics
-
Henyai Sluts
-
Xena Movie
-
16 Year Old Hot Pussy
-
Nikki Nova Pics
-
Teen Girls Pussy Pictures
-
Ms New Booty Song
-
Naked Women Photo
-
Homemade Vudeos
-
Karlie Simon
-
Hardcore Amateur Gay Free Pics
-
Little Young Vigin Porn
-
Young Hot Sexy Male Teens Sex Xxx
-
Nakrd Young Little Girls
-
Free Blackswx Picture
-
Poop Hole
-
Absolute Sex
-
Adolescent Girl
-
Xxx .tv Bitchinmovies.com
Excellent. Job security and spiffy DSL construction in one neat package.
What I did with it:
yes, I can now have a ninja sword operation:
zato =|===- boss
Does code readability or maintainability mean anything to you? The first commenter had it right, “Job Security”.
Nick, I make my career in making Ruby code maintainable and readable through DSLs. You’d think that speaks a little about my stance there. You’ll not see Superators in Adhearsion—they’re a fun thing I cranked out in a weekend to prove I could do it.
Sure, superators are idiomatic, wonky, and weird, but for some things it’s just fun to do that (Camping, anyone?). Like I said in my post, don’t use them where lots of people have to critically read your code. Personal projects? Have at it.
“Use it in your tenderly crafted Ruby DSLs where they make sense.”
This is great. I’ve been wanting a := operator for a long time for assignment semantics for my own classes. However, I’m guessing that := isn’t something that superator can do because of the ’=’ , is that true?
Phil: yeah. Superators is implemented in pure-Ruby and therefore all superators are (perhaps surprisingly) valid Ruby too. If you type it into irb and it reports a SyntaxError, it can’t be a Superator.
Jay, I love what you’re doing here. You are pushing the boundaries of what one can do in straight Ruby in really intriguing ways. Maybe the “will it be used for good or evil crowd” could be placated with a Superator lint tool for pointing out unwise uses of this great power.
The syntax for a valid superator is:
1) One of these binary operators: ** * / % + – << >> & | ^ <=> >= <= < > === == =~
2) Followed by as many of these unary operators: – ~ +
Unfortunately, that rules out the ninja sword and := mentioned before. I was wishing for a _| operator.
I installed the gem and used your examples. At first I got this error:
/superators.rb:4: uninitialized constant String::SuperatorMixin (NameError) from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in ‘gem_original_require’ from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in ‘require’ from TestSup.rb:2
So the only way I could get things to work was to change it up a bit:
require ‘superators/version’ require ‘superators/macro’ require ‘superators/monkey_patch’
class Array include SuperatorMixin extend SuperatorMixin end
Any thoughts on that?
This is crazy! You could make Ruby look like PHP (not that you would want to). Just define ->. I love how expressive Ruby can be. I’m sure many Java programmers will also use this to give themselves the ++ operator!
Rob, were you simply using the examples I posted above? If not, could you paste the code that generated that error?
Also, which version of Ruby are you using? It seems the constant lookup algorithm is strange in your version. If you were doing this on the stock version of Ruby that comes with OSX (version 1.8.2) I would certainly expect some weird issues.
Lance,
Fortunately those are both syntactically invalid Ruby. A binary
++operator is possible with Superators but not a unary one that could emulate the C-like numerical increment operator. The binary++is more like the Erlang concatenation operator.A
->operator is also invalid as well since>is only binary and Ruby will blow up if two binary operators are next to each other.Jay,
I’m using 1.8.6 on Windows XP. The code I originally used was exactly your example code. The code that worked was supplied in my first post starting with the first require statement (It formatted sort of wierd when it posted)
require ‘superators/version’ require ‘superators/macro’ require ‘superators/monkey_patch’
class Array include SuperatorMixin extend SuperatorMixin
superator ”<-” do |operand| self << operand.reverse end end
On a related note, it’s worth looking at the usage of user-defined operators in Haskell for good examples of these in action. Personally, I’d love to see a first-class syntax in Ruby for prefix/infix handling of functions ala Haskell’s. But then Jay would lose an excuse for impressive Demented Ruby Tricks like Superators. ;-)
thats just cool. what i was looking for my DSLs, as you have exactly mentioned. u rock
bye :-) Ashish
hey dude!
cynocrambaceae nonpunishable congregable inturned metalize gentlemanlike unnurtured physiognomical http://cnn.com/2002/WORLD/sailing/10/12/aroundalone.spt/ >De Radigues out of Around Alone http://www.sfcommand.com
Hi http://eumadgfc.com omwss bphmd http://ofefjbxa.com efmgk ljrvu
Hi! tejxy seuen kgthb lywri
Hi! bjtue kgolo asemi uabar
Hi! rwozn fnzil mlrbu hkswf
Hi! odtji vwggp anwso jbtyt
Hi! dmfzh lqujp rmywa ioazp
Hi! xmuwh pnxlg efsll ksxfv
Hi! alicia keys as i am
Hi! exile love mp3
Hello! I think this try.