ThriftUsageRuby

Note: There are bugs in the Ruby generator in Thrift release 20080411p1. These examples are based on SVN rev. 675053

Assuming you're using the definitions in "tutorial.thrift":

Loading the Libraries

require "thrift"
$:.push("/path/to/tutorial/gen-rb")
require "tutorial_constants"

Creating Objects, Using Constants

work = Work.new
work.num1 = 7
work.num2 = 9
work.op = Operation::ADD 

Serializing to/from a Binary String

require "thrift/serializer"
serializer = Thrift::Serializer.new
deserializer = Thrift::Deserializer.new

binary_string = serializer.serialize(work)
# => "\b\000\001\000\000\000\a\b\000\002\000\000\000\t\b\000\003\000\000\000\001\000"

work2 = deserializer.deserialize(Work.new, binary_string)
# => #<Work:0xb7aa16c8 @op=1, @num2=9, @num1=7>
  • No labels