If I have a rails app and I need to put in
require 'xxx'
include yyy
in my class ApplicationController < ActionController::Base
Where within this application.rb file do I put these lines? Thanks, Ike
If I have a rails app and I need to put in
require 'xxx'
include yyy
in my class ApplicationController < ActionController::Base
Where within this application.rb file do I put these lines? Thanks, Ike
It's usually best to put it in the first lines of the file, before any
variable declarations or class definitions. They're a bit like C's
"<includes>" or Java's "imports".
On 10/20/06, Ike <rxv@hotmail.com> wrote:
If I have a rails app and I need to put in
require 'xxx'
include yyyin my class ApplicationController < ActionController::Base
Where within this application.rb file do I put these lines? Thanks, Ike
--
Bira
And be careful where you place include Yyy as the placement influences
where the module will be mixed in.
It's different when you do
include Forwardable
class X
...
end
and
class X
include Forwardable
...
end
Sometimes (=rarely), when you want to require later or not at all,
it's useful to put require inside a method.
On 10/20/06, Bira <u.alberton@gmail.com> wrote:
On 10/20/06, Ike <rxv@hotmail.com> wrote:
> If I have a rails app and I need to put in
> require 'xxx'
> include yyy
>
> in my class ApplicationController < ActionController::Base
>
> Where within this application.rb file do I put these lines? Thanks, IkeIt's usually best to put it in the first lines of the file, before any
variable declarations or class definitions. They're a bit like C's
"<includes>" or Java's "imports".
Bira's kind of right, kind of wrong.
Java imports are package based, which while there is the file system
relation, is trying to keep developers from worrying about file paths.
Ruby requires work like C/C++ #includes. They are purely path related.
However, Ruby does have a system variable available, $:, that is the PATH,
so you can do something like this:
$: << '../../lib/'
require 'some_class_in_lib'
instead of
require '../../lib/some_class_in_lib'
You'll see this type of syntax alot with libraries like Rails, who try to
make everything available without any dealings with file *or* package paths.
Jason
On 10/20/06, Bira <u.alberton@gmail.com> wrote:
On 10/20/06, Ike <rxv@hotmail.com> wrote:
> If I have a rails app and I need to put in
> require 'xxx'
> include yyy
>
> in my class ApplicationController < ActionController::Base
>
> Where within this application.rb file do I put these lines? Thanks, IkeIt's usually best to put it in the first lines of the file, before any
variable declarations or class definitions. They're a bit like C's
"<includes>" or Java's "imports".--
Bira
http://compexplicita.blogspot.com
http://sinfoniaferida.blogspot.com
Jan Svitok wrote:
Sometimes (=rarely), when you want to require later or not at all,
it's useful to put require inside a method.
Just to make it a little clearer, by experimenting you can see:
-------------
def check
a = ['foo', 'bar', 'baz'] * 2
begin
a.each_slice(3) {|slice| p slice}
rescue Exception
puts "No such method\n"
end
require 'enumerator'
a.each_slice(3){|slice| p slice}
end
#Test starts here
begin
x.each_slice(3) {|slice| p slice}
rescue Exception
puts "No such method\n"
end
puts $".include?('enumerator.so')
check
puts $".include?('enumerator.so')
x = ['foo', 'bar', 'baz'] * 2
x.each_slice(2) {|slice| p slice}
---------------
Produces ->
No such method
false
No such method
["foo", "bar", "baz"]
["foo", "bar", "baz"]
true
["FOO", "BAR"]
["BAZ", "FOO"]
["BAR", "BAZ"]
As you can see, 'require' is itself a method (btw, mixed with the
require method of rubygems/custom_require.rb if specified), that calls
a library and adds it to $". Such library is called the first time is
required. So, as Jan said, you may call it anywhere on your program
depending on where do YOU require it.