<?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>Merbist &#187; foundation</title>
	<atom:link href="http://merbist.com/tag/foundation/feed/" rel="self" type="application/rss+xml" />
	<link>http://merbist.com</link>
	<description>Random thoughts of a software developer</description>
	<lastBuildDate>Tue, 03 Jan 2012 02:34:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Undo/Redo in MacRuby</title>
		<link>http://merbist.com/2010/02/02/undoredo-in-macruby/</link>
		<comments>http://merbist.com/2010/02/02/undoredo-in-macruby/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 10:19:44 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[macruby]]></category>
		<category><![CDATA[foundation]]></category>
		<category><![CDATA[NSUndoManager]]></category>
		<category><![CDATA[redo]]></category>
		<category><![CDATA[undo]]></category>

		<guid isPermaLink="false">http://merbist.com/?p=719</guid>
		<description><![CDATA[As I&#8217;m working on my upcoming O&#8217;Reilly MacRuby book, I&#8217;m writing quite a lot of example code. I have spent the last few weeks digging through most of the Foundation framework classes to hopefully make Cocoa more accessible to Ruby developers. In some instances things might look quite weird to someone new to Cocoa in [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;m working on my upcoming O&#8217;Reilly MacRuby book, I&#8217;m writing quite a lot of example code. I have spent the last few weeks digging through most of the Foundation framework classes to hopefully make Cocoa more accessible to Ruby developers.</p>
<p>In some instances things might look quite weird to someone new to Cocoa in some cases, things seem almost too easy. Here is an example implementing a undo/redo functionality using Foundations&#8217; <em><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSUndoManager_Class/Reference/Reference.html">NSUndoManager</a></em>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">framework <span style="color:#996600;">'Foundation'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Player
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:x</span>, <span style="color:#ff3333; font-weight:bold;">:y</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@x</span> = <span style="color:#0066ff; font-weight:bold;">@y</span> = <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> undo_manager
    <span style="color:#0066ff; font-weight:bold;">@manager</span> <span style="color:#006600; font-weight:bold;">||</span>= NSUndoManager.<span style="color:#9900CC;">alloc</span>.<span style="color:#9900CC;">init</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> left
    undo_manager.<span style="color:#9900CC;">prepareWithInvocationTarget</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">right</span>
    <span style="color:#0066ff; font-weight:bold;">@x</span> <span style="color:#006600; font-weight:bold;">-</span>= <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> right
    undo_manager.<span style="color:#9900CC;">prepareWithInvocationTarget</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">left</span>
    <span style="color:#0066ff; font-weight:bold;">@x</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Which you can use as such:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara = Player.<span style="color:#9900CC;">new</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&lt;</span>Player:0x200267c80 <span style="color:#0066ff; font-weight:bold;">@y</span>=<span style="color:#006666;">0</span> <span style="color:#0066ff; font-weight:bold;">@x</span>=<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">undo_manager</span>.<span style="color:#9900CC;">canUndo</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#008000; font-style:italic;"># normal since we did not do anything yet</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">left</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">x</span> <span style="color:#008000; font-style:italic;"># -1</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">undo_manager</span>.<span style="color:#9900CC;">canUndo</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#008000; font-style:italic;"># now we can undo, so let's try</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">undo_manager</span>.<span style="color:#9900CC;">undo</span> <span style="color:#008000; font-style:italic;"># undo back to initial position</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;NSUndoManager:0x200257560&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">x</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">undo_manager</span>.<span style="color:#9900CC;">canUndo</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#008000; font-style:italic;"># we can't anymore which makes sense </span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">undo_manager</span>.<span style="color:#9900CC;">canRedo</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#008000; font-style:italic;"># however we can redo what we just undone</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">undo_manager</span>.<span style="color:#9966CC; font-weight:bold;">redo</span> <span style="color:#008000; font-style:italic;"># redo to before we called undo</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;NSUndoManager:0x200257560&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> lara.<span style="color:#9900CC;">x</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span></pre></div></div>

<p>The above example was tested in macirb but as you can see, actions can be undone and redone very very easily. This is just a quick preview of what you can do using Ruby + Cocoa and hopefully it will give you some cool ideas to implement.</p>
]]></content:encoded>
			<wfw:commentRss>http://merbist.com/2010/02/02/undoredo-in-macruby/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Fun with MacRuby</title>
		<link>http://merbist.com/2009/12/29/fun-with-macruby/</link>
		<comments>http://merbist.com/2009/12/29/fun-with-macruby/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 03:09:45 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[macruby]]></category>
		<category><![CDATA[foundation]]></category>

		<guid isPermaLink="false">http://merbist.com/?p=653</guid>
		<description><![CDATA[To be ready for 2010, I&#8217;m taking some time off relaxing and spending time with my family in Florida. During my free time, I&#8217;ve been reading, catching up on movies and TV shows and worked on the MacRuby book that I am writing for O&#8217;Reilly. I wrote a bunch of small apps, played with various [...]]]></description>
			<content:encoded><![CDATA[<p>To be ready for 2010, I&#8217;m taking some time off relaxing and spending time with my family in Florida.</p>
<p>During my free time, I&#8217;ve been reading, catching up on movies and TV shows and worked on the MacRuby book that I am writing for O&#8217;Reilly.</p>
<p>I wrote a bunch of small apps, played with various APIs and every single time I was amazed by all the goodies Apple makes available to developers. My most recent discovery is very simple but I wanted to share it with you.</p>
<p>I often type text in English, French and Spanish and I even mix the languages from time to time. SnowLeopard comes with a great spellchecker that auto detects the language I&#8217;m typing in and is most of the time correct. It&#8217;s a very impressive feature and I was wondering if, as a MacRuby developer, I could use one of Apple&#8217;s lib to detect what language is being used.  I dug through the documentation but didn&#8217;t find anything. I started looking at some header files and found the API to use <img src='http://merbist.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">framework <span style="color:#996600;">'Foundation'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">String</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> language
    CFStringTokenizerCopyBestStringLanguage<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, CFRangeMake<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>, <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Bonne année!&quot;</span>.<span style="color:#9900CC;">language</span>
<span style="color:#008000; font-style:italic;"># =&amp;gt; &quot;fr&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Happy new year!&quot;</span>.<span style="color:#9900CC;">language</span>
<span style="color:#008000; font-style:italic;"># =&amp;gt; &quot;en&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;¡Feliz año nuevo!&quot;</span>.<span style="color:#9900CC;">language</span>
<span style="color:#008000; font-style:italic;"># =&amp;gt; &quot;es&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Felice anno nuovo!&quot;</span>.<span style="color:#9900CC;">language</span>
<span style="color:#008000; font-style:italic;"># =&amp;gt; &quot;it&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;أعياد سعيدة&quot;</span>.<span style="color:#9900CC;">language</span>
<span style="color:#008000; font-style:italic;"># =&amp;gt; &quot;ar&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;明けましておめでとうございます。&quot;</span>.<span style="color:#9900CC;">language</span>
<span style="color:#008000; font-style:italic;"># =&amp;gt; &quot;ja&quot;</span></pre></div></div>

<p>The documentation says that the result is not guaranteed to be accurate and that typically 200-400 characters are required to reliably guess the language of a string. (<a href="http://developer.apple.com/mac/library/documentation/CoreFoundation/Reference/CFStringTokenizerRef/Reference/reference.html#//apple_ref/c/func/CFStringTokenizerCopyBestStringLanguage" target="_blank">CFStringTokenizer Doc</a>)</p>
<p>Probably not the most useful piece of code, but really cool none the less <img src='http://merbist.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Happy new year!</p>
]]></content:encoded>
			<wfw:commentRss>http://merbist.com/2009/12/29/fun-with-macruby/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

