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

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.

Hello,

On 1 Νοε 2013, at 19:42 , Marc H. [email protected] 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: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

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>

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.

On Sat, Nov 2, 2013 at 3:27 AM, Marc H. [email protected]
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

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

Marc H. 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.

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.