[Ironruby-core] Support For Referencing Ruby Libs As Embedded Resources?

M. David Peterson m.david at xmlhacker.com
Fri Feb 8 21:06:36 EST 2008


On Fri, 08 Feb 2008 18:48:39 -0700, John Messerly <jomes at microsoft.com>  
wrote:

> Is the embedded resource a Ruby file or something else?

Yes, a Ruby file.

> What is the feature used for?

Here's a copy of the pertinent pieces from the related thread on the  
Ruby.NET list.  In short, for ease of redistribution of stdlib source  
files.

On Fri, 08 Feb 2008 18:26:57 -0700, M. David Peterson  
<m.david at xmlhacker.com> wrote:

> On Fri, 08 Feb 2008 13:33:47 -0700, chriso <chrisortman at gmail.com> wrote:

>> Is there a way to bundle any required libraries into the compiled
>> dll?
>
> You could embed a source file into the assembly and then resolve that  
> file internally to your code base.  However, I'm not sure if there is  
> any officially sanctioned way as of yet to reference embedded resources  
> (e.g. res://assemblyname.dll/foo), though this certainly seems like  
> something that needs to be both considered and discussed.
>
> A temporary hack[1,2] _could_ be used  (though I wouldn't recommend this  
> for anything beyond experimentation) via creating an assembly who's only  
> purpose was to return requests for embedded resources to then evaluate  
> the returned Ruby.String, but this would only work for a small fraction  
> of use-cases in which the referenced stdlib didn't reference any other  
> stdlibs.  In this regard there would need to be some way of indicating  
> to the Ruby.NET Runtime whether it should look for an embedded resource,  
> and if yes, where it should look.
>
> I would think that using the standard res://assemblyname.dll/foo/bar  
> would make the most sense.  Anybody happen to have a sense as to how to  
> properly handle this?  I'll make a post to the IronRuby list to see if  
> they plan to provide support for embedded resources, and if so, the  
> syntax they plan to use.
>
>
> [1] C# ResourceManager which returns the Ruby.String for the referenced  
> embedded resource.
>
> using System;
> using System.Resources;
> using System.Text;
>
> namespace ResourceLib
> {
>      public class RubyStdLib
>      {
>          static Encoding m_encoding = Encoding.UTF8;
>          static ResourceManager manager =  
> Properties.Resources.ResourceManager;
>
>          public Ruby.String getStdLib(string stdLibName)
>          {
>              return new  
> Ruby.String(m_encoding.GetString((byte[])manager.GetObject(stdLibName)));
>          }
>      }
> }
>
> [2] Modified Ruby
>
> require 'mscorlib.dll'
> a_date = System::DateTime.new(2007, 1, 1)
> puts a_date.AddYears(1).ToString()
> puts 'Hello .net'
>
> input = %{\
> <% high.downto(low) do |n| # set high, low externally %>
> <%= n %> green bottles, hanging on the wall
> <%= n %> green bottles, hanging on the wall
> And if one green bottle should accidentally fall
> There'd be <%= n - 1
> %> green bottles, hanging on the wall
> <% end %>
> }
> high,low = 10, 8
>
> # Beware of the horrific hack just begging to be exploited that follows.
> # Beyond loading the source file directly or using the taint method is
> # there a better way to handle this that's not immediately obvious?
>
> require 'ResourceLib.dll'
> rm = ResourceLib::RubyStdLib.new
> erb = eval(rm.getStdLib('erb'))::ERB.new(input)
> puts erb
> bind = Kernel.binding
> puts bind
> puts erb.run(bind)
>
> puts 'Bye .net'
>



-- 
/M:D

M. David Peterson
Co-Founder & Chief Architect, 3rd&Urban, LLC
Email: m.david at 3rdandUrban.com | m.david at amp.fm
Mobile: (206) 418-9027
http://3rdandUrban.com | http://amp.fm |  
http://www.oreillynet.com/pub/au/2354


More information about the Ironruby-core mailing list