|
|
#How to use .NET BCL types.
How to Use .NET BCL Types
- This tutorial assumes that you have Ruby.exe installed in a location found in your PATH variable.
This tutorial demonstrates how to use .NET Base Class Library (BCL) types in Ruby code.
Step 1
Create a file named "bcltypes.rb" with the following content:
require 'mscorlib.dll' a_date = System::DateTime.new(2007, 1, 1) puts a_date.AddYears(1).ToString()
The first line is currently required in order to reference BCL core types. Once we have access to the mscorlib library, we can use the DateTime type defined in the System namespace. The second line creates a new instance of the DateTime type which is .NET's core type for representing dates. The last line of code adds a year to our date object and prints the result to the console.
Step 2
Execute the file by typing:
- on Windows
Ruby bcltypes.rb
mono Ruby.exe bcltypes.rb
Ruby.exe is Ruby.NET's equivalent of C-Ruby's ruby.exe. In this example, we simply pass the name of our file as a parameter to the executable. When you do this, Ruby.exe will open the file and execute the code contained therein.
Step 3
You should see the following result in the console:
1/1/2008 12:00:00 AM
Sign in to add a comment

See http://rubydotnet.googlegroups.com/web/Tutorials.htm for more elaborate examples.