How do you use IronRuby to print a report of public methods and public properties of a C# class? - ironruby

Does anyone have a code sample showing how you reflect upon a random c# class from an arbitrary .NET assembly using IronRuby? I am most interested in listing the public methods and properties from a random C# class and .NET assembly.
I have tried to load "System.Reflection" from IronRuby. I can't seem to find a good code sample for doing this.
I know that Ruby can explore the meta data related to a class as well. I, however, can't seem to load my test assembly. (i.e. Test.dll)
Any help that you can offer would be helpful.

In order to load your Test.dll you will just need to require 'Test' assuming its visible to your script.
You can use the built-in Ruby method public_instance_methods but this will include those from Ruby's Object class, e.g.:
System::String.public_instance_methods
If you are only interested in the methods of your CLR type then you can access the Type of a given object through the IronRuby to_clr_type method. This gives you access to all the reflection methods that System.Type offers. For example, the following script will list all the public instance methods of a class Test.MyClass in Test.dll:
$: << 'path/to/dll'
require 'Test'
puts Test::MyClass.to_clr_type.get_methods(
System::Reflection::BindingFlags.Public | System::Reflection::BindingFlags.Instance | System::Reflection::BindingFlags.DeclaredOnly)

Related

How to check where a who calls this method?

I have a custom method in an ABAP class.
I used the 'Where used' tool to show where the class is called from but, as it turns out, it's called from somewhere else I didn't expect.
So what's the best way of showing a complete list of everything that calls the method?
Due to the wonders of object-oriented programming, an instance of a class can hide behind a reference to one of its base classes or interfaces it implements. For example:
DATA foo TYPE REF TO z_my_interface.
CREATE OBJECT foo TYPE z_my_class.
" lots of more code
foo->bar( ).
You can not find this reference to z_my_class->foo with its "Where Used" list, because at that code location foo could also be a reference to an instance of any other class which implements z_my_interface. But you might be able to find this if you don't just look at the where-used list of the method but at the where-used list of the whole class or the interface / base class which declares the method.
And then there are evil dynamic programming tricks like this which determine methods and classes at runtime:
DATA foo TYPE REF TO object.
CONSTANTS: classname TYPE string VALUE 'Z_MY_CLASS',
methodname TYPE string VALUE 'BAR'.
CREATE OBJECT foo TYPE (classname).
CALL METHOD foo->(methodname).
There is no chance to find this with the where-used tool. But if the class- and/or method name does actually appear in the code (it might not, for example if they are read from a customizing table) then you can use the report RS_ABAP_SOURCE_SCAN. This handy little tool allows you to select a set of ABAP programs and search for strings (and even regular expressions) within their sourcecodes.
However, if you know the method gets called when you do something specific as a user and just want to know where, then it can be easier to just set a debugger breakpoint in the method, run into it and check the call stack.
Sorted using the code_scanner transaction.

UML how to represent a class concern/module/extension

I am talking about concern/module/extensions as they exist in Ruby and Swift for example.
A Ruby module is something that a class can include (= add the module functions as its own instance methods) or extend (add the module functions as its own class methods).
A swift extension is also an add-on for class, typically when you want to add a functionality you would first define the prototype, then implement it in an extension.
(please correct me if I'm wrong)
How would you represent such a Ruby module/Swift extension in UML, and its link to the class it is included in/it extends ?
I also don't know a standard for this, but would model it like this:
A Realize relation with an <<import>> stereotype. Maybe the Realize is too strong in the context and a simple Dependency but still with that stereotype would be better.
Not everything is available natively in UML. But like in any language, if you don't have a single word for a thing you can make constructs that describe the thing. You are rather free in choosing your vocabulary. Only you should be consistent in the domain where you use such a paraphrase.

No need to extend class/library in codeigniter

I would like to check if my assumption about codeigniter is right ?
We would normally extend a class when we are trying to include more functionality to the core, such as MY_Controller extends Controller, MY_Model extends Model etc...
But for example, if we are in the checkout library retrieving some checkout info(eg, product_id), we can just $this->load->library('product_lib',array('product_id'=>$product_id)) and we can easily $this->product_lib->product_name etc... from the checkout library right?
The $this->load thing is kind of equivalent to "hard code" checkout library to extend product_lib(class checkout_lib extends product_lib) to be able to use whatever methods/variables there is in the product_lib.
Please enlighten me.
In CodeIgniter $this->load is like having a resource manager (e.g. resourceManager->load("path/to/file")) and it takes care of loading the library, and passing any arguments you specify and such, easily allowing you to quickly get to using it.
So if you have a variable named product_name in your product_lib then yes calling $this->product_lib->product_name will be accessing that variable.
Really it just places the library into an array with the library name as the key and the instance of the library as the value so calling $this->product_lib is really calling something similar to $loadedLibraries['product_lib'] and returning the instance.
I hope that answers what you are asking, I'm quite tired and could have miss understood you question.
I think you misunderstood the OO paradigm and the way CI work.
$this->load is same with instantiate an object of the library/model, or load the helper file. CI have some sort of management to see if the helper/library/model already uploaded or not.
In other hand, the extends is used when defining a class, to tell PHP that the class will be inherit the parent class properties and method. A class is a blue print of object it will produce.
Maybe you can start by understanding the OO concept first. You can read this as a start, and see the reference used there.

Name of class from its object

How to extract a name of the class from its object?
For example I have a #list object which I know is surely an instance of List class. But how do I extract that directly in code?
This kind of information is rather basic Ruby programming. The answer is:
object.class
Extra tip for the next time: try finding this information yourself in the core library documentation. You know you have some kind of object, just start reading the documentation and you will find some method that suites your needs. Information about the methods you can perform on an object can be found here.
If you want to test for an instance of a specific class, I'd go with something like:
#list.is_a?(List)
Like Edwin said, object.class will give you the corresponding Class object. If you just want the name of the class, use object.class.name.

Generate Ruby Classes from XSD

Is there a way to generate Ruby classes (maybe even ActiveResource classes) from an XSD so that they contain a way to serialize the classes to xml valid for the initial XSD?
I know that soap4r has xsd2ruby but it appears that the generated ruby classes cannot be easily serialized to xml.
Shameless self promotion (hope this is okay on stackoverflow) but I'm working on an open source project to do just that
Its still a work in progress (feel free to send patches) but the ultimate goal is to convert XSD to/from Ruby classes (which it does now) and convert XML conforming to that XSD to/from instances of those classes.
Though this was asked a while ago, I came across a solution and thought it might help folks in the future.
My need was similar. I have a .xsd from a colleague and would like to generate a class file from it. My hope is that I'll be able to easily marshall the object and pass it to his RESTful end-point, where his Java server will unmarshall the payload and dynamically build the object on his side with no additional effort.
The solution I found was to get the soap4r from https://github.com/rubyjedi/soap4r. I made the two *.rb files in the bin directory executable and then ran:
bin/xsd2ruby.rb --xsd <source>.xsd --classdef <filename_prefix>
This generated a new file with each of the xsd:complexType implemented as a class. All other complex type were also generated with the correct inheritance relationships and all xsd:element was defined as an instance variable and a class initializer also defined.
Running xsd2ruby.rb by itself yielded the options:
~/src/test/soap4r:bin/xsd2ruby.rb
Usage: bin/xsd2ruby.rb --xsd xsd_location [options]
xsd_location: filename or URL
Example:
bin/xsd2ruby.rb --xsd myapp.xsd --classdef foo
Options:
--xsd xsd_location
--classdef [filenameprefix]
--mapping_registry
--mapper
--module_path [Module::Path::Name]
--force
--quiet
For the sake of completeness, I extended my class with the following (this is a "Prospect" class):
class Prospect
include Enumerable
def each(&block)
self.instance_variables.collect{|v| (v.gsub /#/, '').to_sym }.each(&block)
end
end
This let me use it as the body of an Net::HTTP::Post request.
To the question of a free to_xml: I haven't found it. The ruby Object comes with a to_yaml and to_json out of the box, but I've not found any simple conversion to XML. So it came down to a roll my own "to_xml".
Hope this helps.
It appears that this might work.
require 'xsd/mapping'
XSD::Mapping.obj2xml(xsdBasedObject)

Resources