IronRuby on Silverlight at RubyConf
It’s always a fun time at RubyConf. So, when you have a 9:00am talk, what do you do the night before? Well, hack in new features, of course! At 2:30am, Tomas, John and I removed the last grotesque hack from our Silverlight demo (grab the sources from here):
This is one of the first (to my knowledge) examples of a code-first Silverlight 1.1 application. Most Silverlight applications that generate UIs do so by creating a XAML string that they feed to the XAML parser. When you have a language as beautiful as Ruby, it’s a shame to be creating trees via strings.
Let’s look at the code that generates the Storyboard for the ‘bounce’ effect for the pictures. So instead of this:
<Storyboard x:Name="Timeline1" TargetName="ScaleTransform1"> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="ScaleX"> <SplineDoubleKeyFrame KeyTime="00:00:00.0" Value="0.200"/> <SplineDoubleKeyFrame KeyTime="00:00:00.2" Value="0.935"/> <SplineDoubleKeyFrame KeyTime="00:00:00.3" Value="0.852"/> <SplineDoubleKeyFrame KeyTime="00:00:00.4" Value="0.935"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="ScaleY"> <SplineDoubleKeyFrame KeyTime="00:00:00.0" Value="0.200"/> <SplineDoubleKeyFrame KeyTime="00:00:00.2" Value="0.935"/> <SplineDoubleKeyFrame KeyTime="00:00:00.3" Value="0.852"/> <SplineDoubleKeyFrame KeyTime="00:00:00.4" Value="0.935"/> </DoubleAnimationUsingKeyFrames> </Storyboard>
You can do:
class BounceAnimation < AnimationBase
def initialize(scale_transform_element)
@obj = Wpf.build(Storyboard, :name => random_name,
:target_name => scale_transform_element) {
add(DoubleAnimationUsingKeyFrames, :begin_time=>’00:00:00′,
:target_property => "ScaleX") {
add(SplineDoubleKeyFrame, :key_time => ‘00:00:00.0′, :value => 0.200)
add(SplineDoubleKeyFrame, :key_time => ‘00:00:00.2′, :value => 0.935)
add(SplineDoubleKeyFrame, :key_time => ‘00:00:00.3′, :value => 0.852)
add(SplineDoubleKeyFrame, :key_time => ‘00:00:00.4′, :value => 0.935)
}
add(DoubleAnimationUsingKeyFrames, :begin_time=>’00:00:00′,
:target_property => "ScaleY") {
add(SplineDoubleKeyFrame, :key_time => ‘00:00:00.0′, :value => 0.200)
add(SplineDoubleKeyFrame, :key_time => ‘00:00:00.2′, :value => 0.935)
add(SplineDoubleKeyFrame, :key_time => ‘00:00:00.3′, :value => 0.852)
add(SplineDoubleKeyFrame, :key_time => ‘00:00:00.4′, :value => 0.935)
}
}
end
end
The advantage of the latter is that the animation is a named entity that can be parameterized or composed with other animations. It’s also a great example of how far we have come in IronRuby. If you look at SplineDoubleKeyFrame, we need to convert the string ‘00:00:00.0′ to a KeyTime object. We do so by monkey-patching DoubleKeyFrame, which is a base class of SplineDoubleKeyFrame:
class DoubleKeyFrame
alias_method
ld_key_time=, :key_time=
def key_time=(time_span)
self.old_key_time = KeyTime.from_time_span(TimeSpan.parse(time_span))
end
end
I’m overriding the implementation of the key_time property setter with a custom property setter that does the type conversion. The idea is to define type converters at the point in the inheritance hierarchy where the property is introduced; once you’ve done so, you’ve modified the behavior across the system.
If you look at the code in Silverlight.rb, you’ll see many more examples of these kinds of type converters. In the future, I suspect that the type converters in Silverlight.rb will be generated by reflecting over the WPF APIs in Silverlight. Look for more metaprogramming goodness for Silverlight as both our implementation and Silverlight matures.
Unfortunately, you can’t run these bits yet. We’re using a private build of Silverlight to run this stuff today. But once we (DLR) sync up with the next CTP of Silverlight, you’ll be able to run IronRuby in your browser. Fun times.


05. Nov, 2007 








In-Browser IronRuby in the Next Silverlight CTP
From John Lam: once we (DLR) sync up with the next CTP of Silverlight, youll be able to run
I like the idea, but the XML version looks more concise in this instance. I’d probably use xml builder to create the XAML markup – this would also give you composability.
In-Browser IronRuby in the Next Silverlight CTP
From John Lam: once we (DLR) sync up with the next CTP of Silverlight, youll be able to run
When you say “run Ruby in the browser,” does this mean that we’ll be able to do DOM manipulation via Ruby? Because IMO that’s when the fun begins.
@Dan:
But you now need to round-trip through the XAML parser, and the thing that you are composing would not be Ruby either.
@Jeremy:
Yes- this is definitely a scenario that SL suports through the HTML bridge. There are some interesting ideas that some folks had at RubyConf about building a browser-agnostic DOM + WPF programming layer in Ruby.
True. I guess it depends on the application, and how you want to couple the definition of the WPF tree with its instantiation.