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.
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
n `require'
from (irb):1
from C:/Ruby193/bin/irb:12:in `<main>'
autoload :MyLibrary, 'mylibrary'
=> nil
MyLibrary.new
LoadError: cannot load such file -- mylibrary
from (irb):4
from C:/Ruby193/bin/irb:12:in `<main>'
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
Both ``B.constants(:true)` and `B.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.
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?
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)
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:
Only by reading the documentation. ri is a good start.
···
On 13 March 2013 20:19, Love U Ruby <lists@ruby-forum.com> wrote:
Hans Mackowiak 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?
On 13 March 2013 20:19, Love U Ruby <lists@ruby-forum.com> 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.