Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 172183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:07:14+00:00 2026-05-11T13:07:14+00:00

class Tree def initialize*d;@d,=d;end def to_s;@l||@r?,>:@d;end def total;(@d.is_a?(Numeric)?@d:0)+(@l?@l.total: 0)+(@r?@r.total: 0);end def insert d alias

  • 0
class Tree   def initialize*d;@d,=d;end   def to_s;@l||@r?',>':@d;end   def total;(@d.is_a?(Numeric)?@d:0)+(@l?@l.total: 0)+(@r?@r.total: 0);end   def insert d     alias g instance_variable_get     p=lambda{|s,o|d.to_s.send(o,@d.to_s)&&       (g(s).nil??instance_variable_set(s,Tree.new(d)):g(s).insert(d))}     @d?p[:@l,:]:@d=d   end end 

Would anyone like to take a stab at explaining what this does? It appeared as an answer in a question I asked about code that is too clever. But it’s too clever for me to tell whether it’s simply a joke. If it’s not, I’d be interested to know how it works, should anyone care to explain.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:07:14+00:00Added an answer on May 11, 2026 at 1:07 pm

    EDIT: The person who posted the original obfuscated example gave the actual source code in his answer. He also posted a corrected version of the obfuscated code, because as I noted, some of it didn’t make sense even when you removed the funky syntax.

    That is some nicely obfuscated code. As with most obfuscated code, it’s mostly a lot of ternary operators and a stubborn refusal to put in whitespace where a normal person would. Here is basically the same thing written more normally:

    class Tree   def initialize(*d)     @d,  = d # the comma is for multiple return values,              # but since there's nothing after it,              # all but the first are discarded.   end   def to_s     @l || @r ? ',>' : @d   end   def total     total = @d.is_a?(Numeric) ? @d : 0     total += @l.total if @l     total += @r.total if @r   end   def insert(arg)     if @d       if @l         @l.insert(arg)       else         @l = Tree.new(arg)       end     else       @d = arg     end   end end 

    The insert method is not syntactically valid (it’s missing a method name at one part), but that’s essentially what it does as far as I can tell. The obfuscation in that method is pretty thick:

    1. Instead of just doing @l = whatever, it uses instance_variable_get() and instance_variable_set(). Even worse, it aliases instance_variable_get() to just be g().

    2. It wraps most of the functionality in a lambda function, to which it passes the name of the @l. Then it calls this function with the lesser-known syntax of func[arg1, arg2], which is equivalent to func.call(arg1, arg2).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Tree attr_accessor :children, :node_name def initialize(tree_hash={}) tree_hash.each do |k,v| @node_name = k @children
Lets say i have something like this: This is file tree.py: class leaf(): def
I have a tree of nodes like this: class Node: next # the next
class Node: '''represents a new node in the BST''' def __init__(self,key): self.key=key self.disconnect() def
In django.utils.tree.py: def _new_instance(cls, children=None, connector=None, negated=False): obj = Node(children, connector, negated) obj.__class__ =
So I have the following: class Tree(object): def__init__(self): self.right = None self.left = None
I have such case classes: abstract class Tree case class Sum(l: Tree, r: Tree)
How would I write a map function for an n-ary tree in ruby? class
I have a class that acts as an item in a tree: class CItem(
I have a tree of active record objects, something like: class Part < ActiveRecord::Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.