Confusion with some Module methods

Can anyone help me to understand how the below module methods works?

Module#used()

Module#autolaod (this is documented as - Registers filename to be loaded
(using Kernel::require) the first time that module (which may be a
String or a symbol) is accessed in the namespace of mod.) But the
example given in the official doc not understood.

Module#refine

Thanks in advance

Module#used() & Module#refine are part of the Refinment system … but
is a) not documented, and b) not full working yet

Module#autolaod works like an “require if const is missing”

Hans M. wrote in post #1101201:

Module#used() & Module#refine are part of the Refinment system … but
is a) not documented, and b) not full working yet

Module#autolaod works like an “require if const is missing”

I got a link on Module#autoload as below:

http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html

Hope it will be helpful for me. If any confusion then I will post here.

does not work, because autoload use require, not require_relative, your
mylibrary.rb is not in the LOAD_PATH, so the normal require cant find
it, thats why autoload does not work…

but as you can see, the accessing of an non-existing constant does
result in an require

I created a file say mylibrary.rb. content of which is

puts “I was loaded!”

class MyLibrary
end

Now I tried to load it in my IRB and got the below error.

C:>irb --simple-prompt

require ‘mylibrary’
LoadError: cannot load such file – mylibrary
from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:i
n require' from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:i nrequire’
from (irb):1
from C:/Ruby193/bin/irb:12:in `’

autoload :MyLibrary, ‘mylibrary’
=> nil

MyLibrary.new
LoadError: cannot load such file – mylibrary
from (irb):4
from C:/Ruby193/bin/irb:12:in `’

Hans M. wrote in post #1101355:

does not work, because autoload use require, not require_relative, your
mylibrary.rb is not in the LOAD_PATH, so the normal require cant find
it, thats why autoload does not work…

but as you can see, the accessing of an non-existing constant does
result in an require

That means I need to put it any one of the directory as found below:

irb(main):003:0> $LOAD_PATH
=> [“C:/Ruby193/lib/ruby/site_ruby/1.9.1”,
“C:/Ruby193/lib/ruby/site_ruby/1.9.1/
i386-msvcrt”, “C:/Ruby193/lib/ruby/site_ruby”,
“C:/Ruby193/lib/ruby/vendor_ruby/
1.9.1”, “C:/Ruby193/lib/ruby/vendor_ruby/1.9.1/i386-msvcrt”,
“C:/Ruby193/lib/rub
y/vendor_ruby”, “C:/Ruby193/lib/ruby/1.9.1”,
“C:/Ruby193/lib/ruby/1.9.1/i386-min
gw32”]

Thanks for your hints. I will give it a try.

OR maybe thats why “autoload” is not an good idea?

autoload can also load gems because of require

it maybe an not so good idea to add your file itself to this dirs

C:>irb --simple-prompt --noecho

module A
NAME = “Ayan”
end

module B
AGE = 24
include A
end

p B.include?(A)
true

B::NAME
p B::NAME
“Ayan”

B.constants(:true)
p B.constants(:true)
[:AGE, :NAME]

p B.constants(:false)
[:AGE, :NAME]

Both ``B.constants(:true)andB.constants(:false)` has shown the same
output as
i passed to them symbols which method couldn’t reslove as it is the
design. I understood.

But my question is there any way by which I can test if the method can
recognize the symbol as its argument
or not in advance? asked it out of curiosity.

p B.constants(false)
[:AGE]

:false and :true a both resolved as True in an if cause …
only nil and false itself are treated as false,
[],"",{},0,0.0 and others are true too

Hans M. wrote in post #1101365:

:false and :true a both resolved as True in an if cause …
only nil and false itself are treated as false,
[],"",{},0,0.0 and others are true too

yes @hans thanks for you help. I know these.

But my question was that - In advance is it possible to test if a method
take as its argument symbol or not?

No you cant without calling the method

On 13 March 2013 20:19, Love U Ruby [email protected] wrote:

Only by reading the documentation. ri is a good start.

Matthew K. wrote in post #1101371:

On 13 March 2013 20:19, Love U Ruby [email protected] wrote:

Only by reading the documentation. ri is a good start.

Humm, That I know. But I thought if anything like Module#const_defined?
or Module#class_variable_defined? is there or not. so that I can test
the same for symbols on my IRB.But @Hans confirmed that - nothing is
there. So It’s Okay. :smiley:

Bar.ancestors
#=>[Bar, FooBar, Foo, Object, Kernel, BasicObject]

From one of the online resource I found the below code:

module FooBar
def hello
puts 2
super
end
end

class Foo
def hello
puts ‘hello’
end
end

class Bar < Foo
include FooBar
def hello
puts 1
super
end
end

Bar.new.hello

Output:

#1
#2
#hello
#=> nil

I really did never meet with such techniques. Can anyone help me by
saying what technique it is? How only with super hello method has
been called in chain and produced the outputs?

Thanks in advance!

its not the ancestor of FooBar, only in the view of Bar … you need to
try to understand how #include works …

Bar --includedModule–> FooBar
----superclass----> Foo

as you can see, FooBar and Foo are not connected
(there is an iClass for that so internaly there is an virtual class that
points to FooBar and is also connected to Foo)

Hans M. wrote in post #1101414:

Bar.ancestors
#=>[Bar, FooBar, Foo, Object, Kernel, BasicObject]

Yeah! that I understood.

But how does Foo becomes ancestor of FooBar. I have a confusion
about that.

module FooBar
def hello
puts 2
super
end
end

class Foo
def hello
puts ‘hello’
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs