Tuesday, May 29, 2012

Global variable in Ruby/JRuby

If anybody wants to create a global variable which can be access to all the ruby class and module through out the project. then follow the example given below:

$globalvariable=

class
   .
   .
   .

end


The value of the globalvariale can be access through out the ruby project.

i.e. puts($globalvariable)

NOTE: "$globalvariable=value" may be display as $globalvariable= . it is a Bug with Blogger. So read it as  "$globalvariable=value"

Monday, May 28, 2012

Log Redirection in Ruby

If you wan to redirect your methods log to a file instead of console and after writing the log to the file the output should back to console then follow the below instructions.

def example()
 oldout=$stdout
 $stdout=File.new("",'a')
.
.
.                   
.
.
$stdout=oldout
end


you can change the type of writing to log file with replace of 'w', 'w+' etc.. depends on your requirements.