Skip to content

Instantly share code, notes, and snippets.

@rentalcustard
Created February 25, 2013 15:34
Show Gist options
  • Save rentalcustard/5030640 to your computer and use it in GitHub Desktop.
Save rentalcustard/5030640 to your computer and use it in GitHub Desktop.
What actually is `using`?

A search for 'using' on http://www.ruby-doc.org/core-2.0/index.html returns, well, nothing. So, what is it?

2.0.0p0 :001 > method(:using)
=> #<Method: main.using> 
2.0.0p0 :002 > _.source_location
=> nil

So it's some kind of method on main with no source location. Hmm. Well, the changelog mentions ri ruby:syntax. Let's try that.

~/code/200-test> ri ruby:syntax | cat
= Pages in ruby core

Right. OK. Let's have a look online.

http://www.ruby-doc.org/core-2.0/doc/syntax_rdoc.html

I still have no idea.

@durran
Copy link

durran commented Feb 25, 2013

static VALUE
top_using(VALUE self, VALUE module)
{
    NODE *cref = rb_vm_cref();
    rb_control_frame_t *prev_cfp = previous_frame(GET_THREAD());

    warn_refinements_once();
    if (cref->nd_next || (prev_cfp && prev_cfp->me)) {
    rb_raise(rb_eRuntimeError, "using is permitted only at toplevel");
    }
    Check_Type(module, T_MODULE);
    rb_using_module(cref, module);
    rb_clear_cache();
    return self;
}

@durran
Copy link

durran commented Feb 25, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment