I am using 2 set of scripts for soap communication, inside they are identical classes and variables therefore when using the main script I need to put condition.
if A
require A.rb
if B
require B.rb
The problem is that the ruby2exe does not include these script when compiling the main script.
If have put the 2 require on the top of the file it does not work but then the main script confuse between the classes in the two files.
Idea how to solve it ?
Thanks
Eran
Try to include both at the beginning so the compiler picks them up, later at line +520 you do the include again, i presume the last included file will be what is used.
Also try the ocra compiler which is more recent then ruby2exe.
Related
I'm working on a project in ruby, and I have many source files each declaring a few classes and methods in a module. I've been looking around and I may just be missing something, but I can't seem to find a way to merge all of my source files into a single file. I want a single file because the end product here is meant to be a command line tool, and users won't want to install 20 files in order to run a single command.
Is there any way to take many ruby source files and process all of the require statements ahead of time to create a single file that can be run by itself as a stand-alone program?
A few things that may be useful (or harmful?):
The only file with code that is not within a function is the main file. Therefore, only the main file will have code that is run immediately after the file is parsed.
I require all needed files immediately at the start of each file (after the initial comment), so the require statements are all at the top of the file, and all dependancies are listed at the start of each file
I call require on all files required by each file, regardless of weather or not they may have been included already.
Example (A few files from my project):
<filename> --> <include1>
<include2>
...
build.rb [the main file] --> BuildSystem.rb
Utilities.rb
BuildSystem.rb --> Project.rb
YamlFile.rb
XmlFile.rb
Utilities.rb
Project.rb --> YamlFile.rb
XmlFile.rb
Utilities.rb
What I'm looking for would be something that would allow me to combine all 5 of these files into a single build file that can be installed just by putting it in the right place. Any help would be great, thanks!
The only file with code that is not within a function is the main file. Therefore, only the main file will have code that is run immediately after the file is parsed.
Because of this, you may be able to simply run the following from the shell (assuming a OS X / *nix system):
touch main.rb
cat Utilities.rb >> main.rb
cat XmlFile.rb >> main.rb
cat YamlFile.rb >> main.rb
cat Project.rb >> main.rb
cat BuildSystem.rb >> main.rb
cat build.rb >> main.rb # must be appended last
You can put this into a shell script to "build" your output file each time you make a change.
Using this method, you will have require statements scattered throughout the output main.rb file, but since they are idempotent it won't have any negative effects.
This might be a trivial question, but I've searched and found nothing about it. I'm trying to make a complex application on ruby and I have my classes on different .rb files which the "Main" class requires. As it's written in the code:
require 'Book.rb'
require 'Person.rb'
These files that contains the classes are in the same directory, so what I'm looking for is a Batch command which allows me to include these files, like the one for one file but extended:
ruby Main.rb
Which, of course fails saying that it can't find 'Book.rb' (returns error and doesn't look for the other)
Thank you for your help.
Thanks to the user Abhi y found this post. The command that worked for me was:
irb -I . -r Main.rb
From which you can get further information on the link.
I have to maintain a Ruby script, which requires some libs I don't have locally and which won't work in my environment. Nevertheless I want to spec some methods in this script so that I can change them easily.
Is there an option to stub some of the require statements in the script I want to test so that it can be loaded by rspec and the spec can be executed within my environment?
Example (old_script.rb):
require "incompatible_lib"
class Script
def some_other_stuff
...
end
def add(a,b)
a+b
end
end
How can I write a test to check the add function without splitting the "old_Script.rb" file and without providing the incompatible_lib I don't have?
Instead of stubbing require which is "inherited" from Kernel, you could do this:
Create a dummy incompatible_lib.rb file somewhere that is not in your $LOAD_PATH. I.e., if this is a Ruby application (not Rails), don't put it in lib/ nor spec/.
You can do this a number of ways, but I'll tell you one method: in your spec file which tests Script, modify $LOAD_PATH to include the parent directory of your dummy incompatible_lib.rb.
Ordering is very important -- next you will include script.rb (the file which defines Script).
This will get you around the issue and allow you test test the add method.
Once you've successfully tested Script, I would highly recommend refactoring it so that you don't have to do this technique, which is a hack, IMHO.
Thanks, I also thought about the option of adding the files, but finally hacked the require itself within the test case:
module Kernel
alias :old_require :require
def require(path)
old_require(path) unless LIBS_TO_SKIP.include?(path)
end
end
I know that this is an ugly hack but as this is legacy code executed on a modified ruby compiler I can't easily get these libs running and it's sufficient to let me test my modifications...
I have a Ruby code with different classes in a few files. In one file, I start the execution. This file requires my other files.
Is this a good way to start a ruby code?
When I run the code from a symbolic link, for example DIR2/MyRubyCode is a link to the main file DIR1/MyRubyCode.rb, then my requires will fail. I solved the problem by adding the path DIR1 to $LOAD_PATH before the require, but I think there would be much better ways to do it. Do you have any suggestions about that?
If you're using Ruby 1.9 or greater, user require_relative for your dependencies.
require_relative 'foo_class'
require_relative 'bar_module'
If you want to check if a Ruby file is being 'require'ed or executed with 'ruby MyRubyCode.rb', check the __FILE__ constant.
# If the first argument to `ruby` is this file.
if $0 == __FILE__
# Execute some stuff.
end
As far as the require/$LOAD_PATH issue, you could always use the relative path in the require statement. For example:
# MyRubyCode.rb
require "#{File.dirname(__FILE__)}/foo_class"
require "#{File.dirname(__FILE__)}/bar_module"
Which would include the foo_class.rb and bar_module.rb files in the same directory as MyRubyCode.rb.
I know this is an old question, but there is an updated answer to it, and I wanted to post it:
Starting in a more recent version of Ruby (I'm not sure when), you can require files in the same directory by using the following:
require './foo_class'
require './bar_module'
and it'll load files called foo_class.rb and bar_module.rb in the same directory.
For checking if your file is being required or ran normally, check the other answer.
[I'm just starting with Ruby, but "no question is ever too newbie," so I trudge onwards...]
Every tutorial and book I see goes from Ruby with the interactive shell to Ruby on Rails. I'm not doing Rails (yet), but I don't want to use the interactive shell. I have a class file (first_class.rb) and a Main (main.rb). If I run the main.rb, I of course get the uninitialized constant FirstClass. How do I tell ruby about the first_class.rb?
The easiest way is to put them both in the same file.
However you can also use require, e.g.:
require 'first_class'
You can also use autoload as follows:
autoload :FirstClass, 'first_class'
This code will automatically load first_class.rb as soon as FirstClass is used. Note, however, that the current implementations of autoload are not thread safe (see http://www.ruby-forum.com/topic/174036).
There's another point worth noting: you wouldn't typically use a main file in ruby. If you're writing a command line tool, standard practice would be to place the tool in a bin subdirectory. For normal one-off scripts the main idiom is:
if __FILE__ == $0
# main does here
# `__FILE__` contains the name of the file the statement is contained in
# `$0` contains the name of the script called by the interpreter
#
# if the file was `required`, i.e. is being used as a library
# the code isn't executed.
# if the file is being passed as an argument to the interpreter, it is.
end