Are requirements (s) needed?

I want to have one ruby ​​file that can requireuse all the common dependencies, so that other files can only have one requirein this shared file. For instance; I have foo.rb and bar.rb and allrequires.rb. I want to have a string in require "allrequires.rb"both foo.rb and bar.rb, but bar.rbdoes not need all the required ones.

Does it matter if I use requirea .rb file that really does not require this file? Could this affect performance?

I am currently ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]

Update

It doesn't seem like the best idea to “share” / use everything required in both .rb files. What would be for this?

Now I can think of using a file name in state.

+3
source share
4 answers

Another approach to the conditional requirement, the following script does not give an error in the JSON parser, because it is called require1.rb, in scripts that do not have a name, like require1.rb for script2.rb, the stone is not required

require 'json' if "require1.rb, script2.rb"[File.basename(__FILE__)]

p File.basename(__FILE__)

text = '[{ "name" : "car", "status": "good"}, { "name" : "bus", "status": "bad"},{ "name" : "taxi", "status": "soso"},
  {"noname":"", "status" : "worse"}
]' 
data = JSON.parse(text) 

p data.collect { |item| item['name'] } 

EDIT: here is the version using the array

["require1.rb","script1.rb"].find{|script|require 'json' if script===File.basename(__FILE__)}
+1
source

There are two main performance penalties:

  • The time spent on the implementation itself require. In Ruby 1.9.1 and Ruby 1.9.2, the time taken to execute all requirewas worse than linear scalability — if you doubled the number requires, it took you more than two times — I think it took you four times.
  • Code execution time in file required. If you have code as shown below, then code execution will take a non-trivial amount of time.
class MyClass
  MY_CONSTANT = File.read("data.txt")
end
+3

Yes, there will be speed, you can compare how mutch to consider whether it really matters. Several require me to put the hem in my code so that it does not take up much space.

['green_shoes','Hpricot'].each(&method(:require))

You can also fulfill the conditional requirement, but it would be ugly to have around your code

begin
  data = JSON.parse(text)
rescue
  require 'json_pure' 
  data = JSON.parse(text)
end

So give each rb your own request

0
source

My final decision (in case someone finds this useful):

require.rb is called from web.rb or testweb.rb, rufus.rb or testrufus.rb

called_from=caller[0].split(":")[0]


puts "loading web 'requires' for file: #{called_from} ..." if (["web"].any?{|s| called_from[s]})
puts "loading web 'requires' for file: #{called_from} ..." if (["rufus"].any?{|s| called_from[s]})
puts "loading web 'requires' for file: #{called_from} ..." if (["settings"].any?{|s| called_from[s]})

require 'rubygems'

require 'socket'        if (["web","settings"].any?{|s| called_from[s]})
require 'ruby-growl'    if (["web","settings","rufus"].any?{|s| called_from[s]})
require 'sinatra'       if (["web"].any?{|s| called_from[s]}) 

Thanks to @Andrew for the explanation and @peter for the tip on how to solve this.

0
source

All Articles