<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Steve&#039;s Blog &#187; ruby</title>
	<atom:link href="http://crindigo.com/blog/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://crindigo.com/blog</link>
	<description>Escapades of a web programmer</description>
	<lastBuildDate>Sun, 18 Sep 2011 04:40:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19719</generator>
		<item>
		<title>Mariph</title>
		<link>http://crindigo.com/blog/2009/12/mariph/</link>
		<comments>http://crindigo.com/blog/2009/12/mariph/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 00:44:15 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mariph]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://crindigo.com/blog/?p=42</guid>
		<description><![CDATA[Mariph is a collection of a few classes that can read a subset of Ruby&#8217;s Marshal format. I wrote it back when I worked with RPG Maker XP, and wanted to convert the marshalled data from that program into something &#8230; <a href="http://crindigo.com/blog/2009/12/mariph/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://crindigo.com/code/php/Mariph.phps">Mariph</a> is a collection of a few classes that can read a subset of Ruby&#8217;s <a href="http://ruby-doc.org/core/classes/Marshal.html">Marshal</a> format. I wrote it back when I worked with RPG Maker XP, and wanted to convert the marshalled data from that program into something I could read. In order to do it in Ruby, I would have had to recreate most of the module and class structure defined in RPG Maker, so I instead opted to write a reader in the language most familiar to me &#8211; PHP.</p>
<p>It does not support every feature built in to the marshal format, only the ones that I required at the time. This includes: nil, true, false, int, float, string, symbol, regexp (string), array, hash, object, and user. Hash types are returned as a Mariph_Hash, which is a simple representation of a hash table with arbitrary data for keys. Objects are returned as stdClass, with the property __mariphName set to the name of its Ruby counterpart class. User types look for a PHP class defined with the same name as the Ruby class, with any double colon replaced by a double underscore. It then statically calls the class&#8217;s rubyLoad() method, which should accept the raw data string as its only parameter, and return an instance of the class. See the Table class in the file for an example.</p>
<p>The simplest way to parse in the data is by calling the mariph_load() shortcut function, which saves you the trouble of initializing both the tokenizer and interpreter classes. Each top-level element in the marshalled string becomes an element of the returned array.</p>
<pre class="brush: php; light: true; title: ; notranslate">$data = mariph_load($marshalString);</pre>
<p>The Mariph_Hash class has your standard get() and set() methods, along with a few convenience methods such as iterate(), count() (implementing Countable), each(), and reset().</p>
<pre class="brush: php; title: ; notranslate">
$hash = new Mariph_Hash(array('initial' =&gt; 'data'));
$hash-&gt;set(1, 'one');
$hash-&gt;set(2.5, 'two point five');
$hash-&gt;set('str', 'string');
$hash-&gt;set(array(1, 2, 3), 'one two three');

echo $hash-&gt;get(2.5); // &quot;two point five&quot;

// should work in PHP 5.3
$hash-&gt;iterate(function($value, $key) {
    echo 'Key: ';
    var_dump($key);
    echo 'Value: ';
    var_dump($value);
});

// Alternatively iterate with each()
while (list($key, $value) = $hash-&gt;each()) {
    // do whatever
}

// reset iteration position after using each()
$hash-&gt;reset();
</pre>
<p>I&#8217;m sure there&#8217;s much better ways for designing a custom hash class than what I wrote, but for me it gets the job done. Hopefully this small library can prove useful to some people. <img src='http://crindigo.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://crindigo.com/blog/2009/12/mariph/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

