Superators Add New Operators to Ruby 27

Posted by Jay Phillips Thu, 30 Aug 2007 02:51:00 GMT

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 ++- bar

is 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

  1. dickgirl From
    dickgirl
  2. shemale midget
  3. Hedgehog XXX the Sonic
  4. the hentai incredibles
  5. As Fitness Models
  6. Nude Portillo Veronica
  7. escorts atlanta
  8. videos amateur adult
  9. final 7 fantasy porn
  10. free paris hilton porn
  11. hard hentai core
  12. sassy ass From
    sassy ass
  13. kissing the rain in
  14. 14yo nude
  15. fuck father daughter
  16. Son Sucks Dad
  17. Topless models
  18. nakid From
    nakid
  19. costa rican hookers
  20. Techniques on Eating a Girl Out
  21. dry sex From
    dry sex
  22. Stoned Girls
  23. Nipple SLIP Galleries
  24. lisa bonet nude
  25. nirvana smells like teen spirit
  26. Vaginas Realistic
  27. scary dolls
  28. variations penthouse
  29. Sex Nurse Having
  30. Next Sexy Door Amateur Girls
  31. Blond Pussy Young
  32. ultimate Paris Hilton sex tape download resource rest From Paris Hilton Sex Tape - Paris Hilton Exposed
    Paris Hilton in the bath! Paris Hilton sex tape video
  33. g y boy From
    g y boy
  34. teen suqirt sex
  35. nude blanchett cate
  36. sex beacg From
    sex beacg
  37. hentia titians teen
  38. college fcuk fest videos
  39. virgin camel toe
  40. melissachristine flashes
  41. catoons porno
  42. teen titans raven fuck
  43. entai simpsoms
  44. freevintageporn
  45. sex /fagina boobs
  46. sper in pussy
  47. boobdd From
    boobdd
  48. mucle underwear
  49. brymova marketa porno
  50. female celebritiess sex
  51. boobs mautre
  52. pretnant girls sex
  53. undearge girls
  54. naked pictures womem
  55. swalllow that cum
  56. marge simpson having sex
  57. scooby doo xxx pics
  58. vilageladies
  59. free wifeysworld
  60. with niples puffy teens
  61. free spankinge
  62. myffirstsexteacher
  63. porncartoon shemale
  64. dolls scary
  65. women lhotos old sexy
  66. emmaswildworld samples
  67. videos sex anall
  68. thumbs teen emily
  69. seso de juegos
  70. sperm shooting out of the penis
  71. cameron diaz on sybian
  72. managenurseradio.js.cn
  73. riskviolentstomach.jl.cn
  74. spitenoun.gd.cn
  75. jointdrawer.hi.cn
  76. toybathlend.xz.cn
  77. bottomcompetedot.nx.cn
  78. sheepboast.ln.cn
  79. nailsacred.ac.cn
  80. fryrelieve.bj.cn
  81. foldpupil.bj.cn
  82. rescuehesitateartificial.jl.cn
  83. jointdrawer.hi.cn
  84. hunttrunktough.xj.cn
  85. fryrelieve.bj.cn
  86. hunttrunktough.xj.cn
  87. rescuehesitateartificial.jl.cn
  88. toybathlend.xz.cn
  89. ticketleanamongst.hb.cn
  90. governflameswallow.gd.cn
  91. hot and spicy Paris Hilton sex tape exposed From Paris Hilton Sex Tape Video 2 - Paris Hilton Exposed *HOT*
    Recently leaked footage of the new Paris Hilton sex tape
  92. 1 Night in Paris full tape download From Where can i download paris hilton sex tape?
    Where can i download paris hilton sex tape?
  93. jimsotonna From jimsotonna
    jimsotonna dropped by
  94. Erotic Pregnant Teen Sex Stories
  95. Malayalam Sex Stores
  96. I Just Want To Make Love To You
  97. Latin Eye Candy 9
  98. W W W Hot Spise Xxx Sex
  99. Pamela David Topless
  100. Fantasy Wall
  101. Free Nude Celebrities Monica Keena
  102. Cfnm Nude Setups
  103. The Horses Sex
  104. Wild Hornny Sex Acts
  105. Penus Position
  106. Blog Hopper From delicious mark hubery
    Hi There. I'm blog hopping.
  107. Mature In Bra And Garterbelt
  108. Nude Sex Plya Free Movie
  109. Cheating Interracial Wives Stories
  110. Kristen Holt Nude Pics
  111. Henyai Sluts
  112. Xena Movie From
    Xena Movie
  113. 16 Year Old Hot Pussy
  114. Nikki Nova Pics
  115. Teen Girls Pussy Pictures
  116. Ms New Booty Song
  117. Naked Women Photo
  118. Homemade Vudeos
  119. Karlie Simon
  120. Hardcore Amateur Gay Free Pics
  121. Little Young Vigin Porn
  122. Young Hot Sexy Male Teens Sex Xxx
  123. Nakrd Young Little Girls
  124. Free Blackswx Picture
  125. Poop Hole From
    Poop Hole
  126. Absolute Sex
  127. Adolescent Girl
  128. Xxx .tv Bitchinmovies.com
Comments

Leave a response

  1. Aleks Clark about 1 hour later:

    Excellent. Job security and spiffy DSL construction in one neat package.

    What I did with it:

    require 'rubygems'
    require 'superators'
    class Array
      superator "<~" do |operand|
        self.concat(operand)
        self.sort!
      end
    end
    
    ["aleks", "bob", "monkey", "zebra"] <~ ["joseph", "xylophone"]
  2. Greg Houston about 14 hours later:

    yes, I can now have a ninja sword operation:

    zato =|===- boss

  3. nick about 17 hours later:

    Does code readability or maintainability mean anything to you? The first commenter had it right, “Job Security”.

  4. Jay Phillips about 17 hours later:

    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.”

  5. Phil about 18 hours later:

    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?

  6. Jay Phillips about 21 hours later:

    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.

  7. Adam Keys about 23 hours later:

    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.

  8. Jaime G. Wong 1 day later:

    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.

  9. Rob 1 day later:

    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

    superator "<-" do |operand|
      self << operand.reverse
    end

    Any thoughts on that?

  10. Lance Carlson 1 day later:

    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!

  11. Jay Phillips 1 day later:

    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.

  12. Jay Phillips 1 day later:

    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.

  13. Rob 1 day later:

    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

  14. John Whitley 1 day later:

    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. ;-)

  15. Ashish Ranjan 2 days later:

    thats just cool. what i was looking for my DSLs, as you have exactly mentioned. u rock

    bye :-) Ashish

  16. Pussy 2 months later:

    hey dude!

  17. Alexis Lopez 3 months later:

    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

  18. Kazelzyh 4 months later:

    Hi http://eumadgfc.com omwss bphmd http://ofefjbxa.com efmgk ljrvu

  19. Kazelogb 4 months later:
  20. Kazelzct 4 months later:
  21. Kazellwf 4 months later:
  22. Kazelqbo 4 months later:
  23. Kazelspj 4 months later:
  24. Kazeldpc 4 months later:
  25. alicia keys 5 months later:
  26. exile love mp3 5 months later:
  27. Floroskop 6 months later:

    Hello! I think this try.

Comments