Archive for the ‘Ruby’ Category
Fountain code – solution
The solution to the Fountain code challenge is up on my Github page at https://github.com/carl-ellis/Fountain .
Thank to all who attempted the challenge!
C
Ruby gem – json_serialisable
I was working on a ruby project and stumbed upon my first valid application of metaprogramming. I was creating json serialisation methods and realised they were all practically identical. So I looked at how attr_accessor worked and then wrote my own class method called attr_serialisable. This method generated serialisation methods automatically.
Example
Given a class A
class A
attr_accessor :a, :b, :c
attr_serialisable :a, :b, :c
def initialize(a, b, c)
@a, @b, @c = a, b, c
end
end
attr_serialisable would generate the following methods:
def to_json(*a)
{
"json_class" => self.class.name,
"a" => @a,
"b" => @b,
"c" => @c
}.to_json(*a)
end
def json_create(o)
new(*o["a"], *o["b"], *o["c"])
end
Which will allow the class to easily be used by the ‘json’ library.
Links
Programming languages course
So I ran a talk on learning programming languages last week. It was the second time I had done that particular talk, and in this case the hardware setup went smoothly – as it was done by Stephen Wattam the CSLU VP.
We had a pretty good turn out, mostly of year year undergraduate students who so far had only played with a little C. I took pictures of everyone hard at work doing their task … well, ok. They were mostly on tryruby.org which was even better.
It showed that they had an interest in a new language which is fairly good at prototyping and will allow them to try out their ideas fast. I may have semi pushed them on to it in my talk, so I’m glad they were listening. No one tried learning Haskell though, but then I’ll drop in on everyone next term and see how they are doing. The slides and some info for my talk can be foundĀ at the CSLU site.
On a side note … My Instagram tshirt came! I’m not sure if I’ll ever wear it outside as it’s a bit long, but still!



