Forum: Ruby Ruby automatically include (or run) code on require-action

4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2013-11-01 19:42
Hi.

I have this module:

module Foo
  def test
    puts 'Hi from test() from module Foo.'
  end
end

I put it into file 'foo.rb'

I require this file.

Now, in order to make use of test() method, I would do:

include Foo
test

This works. Is there a way to automatically do "include Foo" upon
require?

I would like to have this as I could eliminate a lot of lines of include
code.
A7fca2db6ff3f4583df98043bd5166f9?d=identicon&s=25 Panagiotis Atmatzidis (Guest)
on 2013-11-01 22:34
(Received via mailing list)
Attachment: signature.asc (840 Bytes)
Hello,

On 1 Νοε 2013, at 19:42 , Marc Heiler <lists@ruby-forum.com> wrote:

> I put it into file 'foo.rb'
>
> I require this file.
>
> Now, in order to make use of test() method, I would do:
>
> include Foo
> test
>
> This works. Is there a way to automatically do "include Foo" upon
> require?

You mean like a keybord shortcut on $editor (vim?) or something?

> I would like to have this as I could eliminate a lot of lines of include
> code.
>
> --
> Posted via http://www.ruby-forum.com/.


Panagiotis (atmosx) Atmatzidis

email:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-11-01 22:50
You mean like this?

test.rb
______________

module Foo
  def test
    puts 'Hi from test() from module Foo.'
  end
end

include Foo
test
______________


irb(main):001:0> load 'C:\Users\Joel\Desktop\test.rb'
Hi from test() from module Foo.
=> true
irb(main):002:0> test
Hi from test() from module Foo.
=> nil
irb(main):003:0>
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2013-11-02 03:27
Hi,

Yes, but you used include.

I want it to be auto-included on require.

Right now I have code in where I do precisely that.

  require 'foo'
  include Foo

My end goal is to do something like this instead:

  require 'foo'
  include Foo # ok this is the default, and now:

  require 'foo/autoinclude' # This here should be like the above. (I
have not decided on the name "autoinclude" yet, this is just for
illustration purposes.)

Is this even possible? I know one could probably patch Kernel#require
but then I would have to carry my modifications to other people too and
that is not possible.

I would need some way to do this all based on required.

Is this doable at all?

> You mean like a keybord shortcut on $editor (vim?) or something?

Hi. No keyboard shortcut or editor - only in pure ruby code.
3df767279ce7d81db0a5bb30f5136863?d=identicon&s=25 Matthew Kerwin (mattyk)
on 2013-11-02 03:44
Marc Heiler wrote in post #1126288:
> Hi,
>
> Yes, but you used include.
>
> I want it to be auto-included on require.

I'd like to know your motivation.  Why do you want it to auto-include
the module?  Or, more importantly: why do you want it to be in a module?

Compare:

--- foo1.rb -----
module Foo
  def bar() 42; end
end
--- bar1.rb -----
require 'foo1'
include Foo
bar
--- end ---------

--- foo2.rb -----
module Foo
  def bar() 42; end
end
include Foo
--- bar2.rb -----
require 'foo2'
bar
--- end ---------

--- foo3.rb -----
def bar() 42; end
--- bar3.rb -----
require 'foo3'
bar
--- end ---------

But to answer your question: there's no practical way to have an
include-on-require trigger, because there is no rule that says how many
modules are defined in a given source file, or what they might be
called, or whether they might not be reopened pre-existing modules, etc.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-11-02 14:10
(Received via mailing list)
On Sat, Nov 2, 2013 at 3:27 AM, Marc Heiler <lists@ruby-forum.com>
wrote:

> Hi,
>
> Yes, but you used include.
>

Yes, but Joel did it in the required file.

I want it to be auto-included on require.
>

$ ruby -e 'require "./a"; test'
Yeah!
$ cat a.rb
module Foo
  def test; puts "Yeah!" end
end

include Foo

$

Kind regards

robert
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2014-01-06 15:49
> I'd like to know your motivation.  Why do you want it to
> auto-include  the module?  Or, more importantly: why do you
> want it to be in a module?

As I wrote before:

-> I would like to have this as I could eliminate a lot of
   lines of include code.


> there's no practical way to have an  include-on-require trigger,
> because there is no rule that says how many modules are defined
> in a given source file, or what they might be called, or whether
> they might not be reopened pre-existing modules, etc.

But I know what rules should be defined. I would like this
to be valid only for when I require something in a given .rb
file. In other words, I would like to have functionality like
"I want to require a certain module, but only into a specific
class". This is possible in ruby when you use require, and
then include, but it seems as if I can not combine the two
in one line. I think python allows more power upon require/import
time than ruby here.

A more advanced Example I would like to aim for:

Consider a file 'foo.rb'

------------------
class Foo
end
------------------

Now I have a module called 'bar.rb'
------------------
module Bar
  def test; puts 'hi from test, defined in module Bar';end
end
------------------

Currently I would have to do this in file 'foo.rb':

------------------
require 'bar'
class Foo
  include Bar
end
------------------

The layout of my projects is always the same, so in all examples
like the above, the explicit include Bar lines are superfluous
and unnecessary to me.

My idea would be to allow something like this:

------------------
require 'bar/class_include'
class Foo
  # include Bar # <--- and this here would be automatic
end
------------------

In other words, it would allow me to do auto-include on
classes.

Is this possible somehow?

It seems that the best I can do is to include a module
upon require time but only into the general top scope
namespace (the explicit include Bar in file bar.rb).
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2014-01-06 15:54
It sounds dangerous to me. Without specifying "include" inside the
specific class, you'd end up including your module into EVERY class!
This is why you need to explicitly include a module, so it is assigned
to the right class.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.