<?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>IONCANNON &#187; ruby</title>
	<atom:link href="http://www.ioncannon.net/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ioncannon.net</link>
	<description>Thoughts on Software Development and Engineering</description>
	<lastBuildDate>Tue, 03 Jan 2012 13:59:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<atom:link rel='hub' href='http://www.ioncannon.net/?pushpress=hub'/>
		<item>
		<title>Mingruby 0.1.8 released</title>
		<link>http://www.ioncannon.net/ruby/111/mingruby-018-released/</link>
		<comments>http://www.ioncannon.net/ruby/111/mingruby-018-released/#comments</comments>
		<pubDate>Sun, 17 Dec 2006 19:41:33 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/ruby/111/mingruby-018-released/</guid>
		<description><![CDATA[I have brought the Ming Ruby library up to date with Ming 0.3, added patches submitted by users over the past year and included a ton of user supplied examples. I hope to find time soon to include the real examples on a page by themselves with the code needed to generate each. You can [...]]]></description>
			<content:encoded><![CDATA[<p>I have brought the Ming Ruby library up to date with Ming 0.3, added patches submitted by users over the past year and included a ton of user supplied examples. I hope to find time soon to include the real examples on a page by themselves with the code needed to generate each. You can check it out on rubyforge: <a href="http://mingruby.rubyforge.org/">Ming Ruby 0.1.8</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/ruby/111/mingruby-018-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Streaming Flash Video Player Using Ming PHP or Ruby</title>
		<link>http://www.ioncannon.net/ruby/108/flash-video-steam-ming-php-ruby/</link>
		<comments>http://www.ioncannon.net/ruby/108/flash-video-steam-ming-php-ruby/#comments</comments>
		<pubDate>Wed, 29 Nov 2006 19:52:32 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/ruby/108/flash-video-steam-ming-php-ruby/</guid>
		<description><![CDATA[I mentioned in creating Flash videos using FFMpeg that you could use Ming to create your own Flash video player. I&#039;ve added a patch to the ruby -ming extension for video streaming so now it is possible to create a streaming player with both PHP and Ruby using their Ming extensions. The following examples show [...]]]></description>
			<content:encoded><![CDATA[<p>I mentioned in <a href="http://www.ioncannon.net/linux/105/create-flash-videos-ffmpeg/">creating Flash videos using FFMpeg</a> that you could use Ming to create your own Flash video player. I&#039;ve added a patch to the <a href="http://www.ioncannon.net/ruby/107/ruby-ming-extension-patch-to-add-video-streaming/">ruby -ming extension for video streaming</a> so now it is possible to create a streaming player with both PHP and Ruby using their Ming extensions. The following examples show you how.<br />
<span id="more-108"></span></p>
<p>First a little background. You can read about <a href="http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001107.html">playing back external FLV files dynamically</a> with actionscript at the Macromedia website. It includes references to the <a href="http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001587.html">NetConnect class</a> and <a href="http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001589.html">NetStream class</a> that together let you stream a FLV file. The main thing Ming does for you in the following simple examples is give you a SWF with a video player in it and let you attach some action script to it.</p>
<p>Here is how you do it in PHP:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<a href="http://www.php.net/ming_setscale"><span class="kw3">Ming_setScale</span></a><span class="br0">&#40;</span>10<span class="sy0">.</span>0000000<span class="br0">&#41;</span><span class="sy0">;</span><br />
<a href="http://www.php.net/ming_useswfversion"><span class="kw3">ming_useswfversion</span></a><span class="br0">&#40;</span>7<span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="re0">$movie</span> <span class="sy0">=</span> <span class="kw2">new</span> SWFMovie<span class="br0">&#40;</span>7<span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="re0">$movie</span><span class="sy0">-&gt;</span><span class="me1">setDimension</span><span class="br0">&#40;</span>320<span class="sy0">,</span>240<span class="br0">&#41;</span><span class="sy0">;</span> <br />
<span class="re0">$movie</span><span class="sy0">-&gt;</span><span class="me1">setBackground</span><span class="br0">&#40;</span>0&#215;33<span class="sy0">,</span>0&#215;33<span class="sy0">,</span>0&#215;33<span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="re0">$movie</span><span class="sy0">-&gt;</span><span class="me1">setRate</span><span class="br0">&#40;</span>8<span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="re0">$strAction</span> <span class="sy0">=</span> <span class="st0">&quot;<br />
stop();<br />
netConn=new NetConnection();<br />
netConn.connect(null);<br />
vStream=new NetStream(netConn);<br />
video1.attachVideo(vStream);<br />
vStream.setBufferTime(10);<br />
vStream.play(&#039;http://localhost/test.flv&#039;);<br />
&quot;</span><span class="sy0">;</span></p>
<p><span class="re0">$stream</span> <span class="sy0">=</span> <span class="kw2">new</span> SWFVideoStream<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="re0">$item</span><span class="sy0">=</span><span class="re0">$movie</span><span class="sy0">-&gt;</span><span class="me1">add</span><span class="br0">&#40;</span><span class="re0">$stream</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="re0">$item</span><span class="sy0">-&gt;</span><span class="me1">setname</span><span class="br0">&#40;</span><span class="st0">&quot;video1&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="re0">$movie</span><span class="sy0">-&gt;</span><span class="me1">add</span><span class="br0">&#40;</span><span class="kw2">new</span> SWFAction<span class="br0">&#40;</span><span class="re0">$strAction</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="re0">$movie</span><span class="sy0">-&gt;</span><span class="me1">nextFrame</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="re0">$movie</span><span class="sy0">-&gt;</span><span class="me1">save</span><span class="br0">&#40;</span><span class="st0">&quot;videostream.swf&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<p>And here is how you do it with Ruby:</p>
<div class="codesnip-container" >
<div class="ruby codesnip" style="font-family:monospace;"><span class="co1">#!/usr/bin/env ruby</span></p>
<p><span class="kw3">require</span> <span class="st0">&#039;ming/ming&#039;</span><br />
<span class="kw1">include</span> Ming</p>
<p>Ming::set_scale<span class="br0">&#40;</span><span class="nu0">10.0000000</span><span class="br0">&#41;</span></p>
<p>movie = SWFMovie.<span class="me1">new</span><span class="br0">&#40;</span><span class="nu0">7</span><span class="br0">&#41;</span><br />
movie.<span class="me1">set_background</span><span class="br0">&#40;</span>0xff, 0xff, 0xff<span class="br0">&#41;</span><br />
movie.<span class="me1">set_dimension</span><span class="br0">&#40;</span><span class="nu0">320</span>, <span class="nu0">240</span><span class="br0">&#41;</span><br />
movie.<span class="me1">set_rate</span><span class="br0">&#40;</span><span class="nu0">8</span><span class="br0">&#41;</span></p>
<p>strAction = <span class="st0">&#039;<br />
stop();<br />
netConn=new NetConnection();<br />
netConn.connect(null);<br />
vStream=new NetStream(netConn);<br />
video1.attachVideo(vStream);<br />
vStream.setBufferTime(10);<br />
vStream.play(<span class="es0">\&#039;</span>http://localhost/test.flv<span class="es0">\&#039;</span>);<br />
&#039;</span></p>
<p>vstream = SWFVideoStream.<span class="me1">new</span><br />
item = movie.<span class="me1">add</span><span class="br0">&#40;</span>vstream<span class="br0">&#41;</span></p>
<p>item.<span class="me1">set_name</span><span class="br0">&#40;</span><span class="st0">&quot;video1&quot;</span><span class="br0">&#41;</span><br />
movie.<span class="me1">add</span><span class="br0">&#40;</span>SWFAction.<span class="me1">new</span><span class="br0">&#40;</span>strAction<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
movie.<span class="me1">next_frame</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>movie.<span class="me1">save</span><span class="br0">&#40;</span><span class="st0">&quot;videostream.swf&quot;</span><span class="br0">&#41;</span></div>
</div>
<p>Here is the result of the above examples:</p>
<div>
<object type="application/x-shockwave-flash" data="http://d28nuaxr58rcpu.cloudfront.net/ming/videostream.swf" width="192" height="154" id="go"><param name="movie" value="http://d28nuaxr58rcpu.cloudfront.net/ming/videostream.swf" /><param name="quality" value="high" /></object>
</div>
<p>Tags: <a href="http://technorati.com/tag/flash" rel="tag">flash</a>, <a href="http://technorati.com/tag/php" rel="tag"> php</a>, <a href="http://technorati.com/tag/ruby" rel="tag"> ruby</a>, <a href="http://technorati.com/tag/flv" rel="tag"> flv</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/ruby/108/flash-video-steam-ming-php-ruby/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Ruby Ming Extension Patch to Add Video Streaming</title>
		<link>http://www.ioncannon.net/ruby/107/ruby-ming-extension-patch-to-add-video-streaming/</link>
		<comments>http://www.ioncannon.net/ruby/107/ruby-ming-extension-patch-to-add-video-streaming/#comments</comments>
		<pubDate>Mon, 27 Nov 2006 15:52:47 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/ruby/107/ruby-ming-extension-patch-to-add-video-streaming/</guid>
		<description><![CDATA[After playing with the Ruby Ming extension a little more I found that they don&#039;t have support for SWFVideoStreams so I made a patch to add it. The patch also fixes the beta issues I described in building the ming ruby extension. Here are the steps you need to follow to patch and install the [...]]]></description>
			<content:encoded><![CDATA[<p>After playing with the Ruby Ming extension a little more I found that they don&#039;t have support for SWFVideoStreams so I made a patch to add it. The patch also fixes the beta issues I described in <a href="http://www.ioncannon.net/ruby/62/building-the-ming-ruby-extension/">building the ming ruby extension</a>.<br />
<span id="more-107"></span></p>
<p>Here are the steps you need to follow to patch and install the latest Ming Ruby extension:</p>
<ul>
<li>Download the 0.1.7 version from: wget http://rubyforge.org/frs/download.php/1841/ming-ruby-0.1.7.tar.gz
  </li>
<li>Uncompress the archive file: tar xvzf ming-ruby-0.1.7.tar.gz</li>
<li>Change into the source directory: cd ming-ruby-0.1.7 </li>
<li>Download the new SWFVideoStream file and stick it into the ext/ming/mingc directory: wget -O ext/ming/mingc/swfvideostream.c http://d28nuaxr58rcpu.cloudfront.net/mingruby/swfvideostream.c</li>
<li>Download the patch: wget http://d28nuaxr58rcpu.cloudfront.net/mingruby/patch</li>
<li>Apply the patch: patch -p0 -u < patch</li>
<li>Run the setup script: ruby setup.rb</li>
</ul>
<p>If you want the source and patch by themselves you can find get those from: <a href="http://d28nuaxr58rcpu.cloudfront.net/mingruby/swfvideostream.c">swfvideostream.c</a> and <a href="http://d28nuaxr58rcpu.cloudfront.net/mingruby/patch">patch</a></p>
<p>And here is an example of using the SWFVideoStream to embed a Flash video into your Flash movie:</p>
<div class="codesnip-container" >
<div class="ruby codesnip" style="font-family:monospace;"><span class="co1">#!/usr/bin/env ruby</span></p>
<p><span class="kw3">require</span> <span class="st0">&#039;ming/ming&#039;</span><br />
<span class="kw1">include</span> Ming</p>
<p>Ming::set_scale<span class="br0">&#40;</span><span class="nu0">10.0000000</span><span class="br0">&#41;</span></p>
<p>m = SWFMovie.<span class="me1">new</span><span class="br0">&#40;</span><span class="nu0">7</span><span class="br0">&#41;</span><br />
m.<span class="me1">set_background</span><span class="br0">&#40;</span>0xff, 0xff, 0xff<span class="br0">&#41;</span><br />
m.<span class="me1">set_dimension</span><span class="br0">&#40;</span><span class="nu0">320</span>, <span class="nu0">240</span><span class="br0">&#41;</span><br />
m.<span class="me1">set_rate</span><span class="br0">&#40;</span><span class="nu0">8</span><span class="br0">&#41;</span></p>
<p>vstream = SWFVideoStream.<span class="me1">new</span><span class="br0">&#40;</span><span class="st0">&#039;test.flv&#039;</span><span class="br0">&#41;</span><br />
vstream.<span class="me1">set_dimension</span><span class="br0">&#40;</span>320, 240<span class="br0">&#41;</span></p>
<p>m.<span class="me1">add</span><span class="br0">&#40;</span>vstream<span class="br0">&#41;</span></p>
<p>0.<span class="me1">upto</span><span class="br0">&#40;</span>vstream.<span class="me1">get_num_frames</span><span class="br0">&#41;</span> <span class="kw1">do</span><br />
&nbsp; m.<span class="me1">next_frame</span><br />
<span class="kw1">end</span></p>
<p>m.<span class="me1">save</span><span class="br0">&#40;</span><span class="st0">&quot;test.swf&quot;</span><span class="br0">&#41;</span></div>
</div>
<p>The above example assumes you have a Flash video file available named test.flv. If you don&#039;t have one you can make one by reading about <a href="http://www.ioncannon.net/linux/105/create-flash-videos-ffmpeg/">creating flash videos with FFMpeg</a>.</p>
<p>Tags: <a href="http://technorati.com/tag/ruby" rel="tag">ruby</a>, <a href="http://technorati.com/tag/ming" rel="tag"> ming</a>, <a href="http://technorati.com/tag/flash" rel="tag"> flash</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/ruby/107/ruby-ming-extension-patch-to-add-video-streaming/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Building The Ming Ruby Extension</title>
		<link>http://www.ioncannon.net/ruby/62/building-the-ming-ruby-extension/</link>
		<comments>http://www.ioncannon.net/ruby/62/building-the-ming-ruby-extension/#comments</comments>
		<pubDate>Sat, 25 Nov 2006 16:06:25 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/seo/62/building-the-ming-ruby-extention/</guid>
		<description><![CDATA[I&#039;ve been trying out Ming SWF output library in PHP for a few days and I thought I would give the Ruby extension a try to see how well it worked. It turns out that it is kind of old and busted but it is fixable. First you need to download Ming version 0.3.0 and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#039;ve been trying out <a href="http://ming.sourceforge.net/">Ming SWF output library</a> in PHP for a few days and I thought I would give the Ruby extension a try to see how well it worked. It turns out that it is kind of old and busted but it is fixable.<br />
<span id="more-62"></span></p>
<p>First you need to download Ming version 0.3.0 and build it. </p>
<p>Once you have the Ming libraries installed you will want to download the <a href="http://mingruby.rubyforge.org/">Ming/Ruby</a> source from rubyforge. If you try to build the source from the instructions you will get a couple errors because the latest version of the Ruby extension was made for a beta version of Ming and is no longer compatible. Luckily there isn&#039;t too much wrong and <a href="http://sourceforge.net/mailarchive/message.php?msg_id=15184915">someone has already figured out how to fix what is wrong</a>. To save you the hop here it is again:</p>
<p>Edit swfmovie.c under the ext/ming/mingc/ directory with the following:</p>
<div class="codesnip-container" >
<div class="c codesnip" style="font-family:monospace;">len <span class="sy0">=</span> SWFMovie_output<span class="br0">&#40;</span>m<span class="sy0">-&gt;</span>this<span class="sy0">,</span> simpleOutputMethod<span class="sy0">,</span> NULL<span class="sy0">,</span> level<span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p>on line 231 should be replaced with:</p>
<div class="codesnip-container" >
<div class="c codesnip" style="font-family:monospace;">Ming_setSWFCompression<span class="br0">&#40;</span>level<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp;len <span class="sy0">=</span> SWFMovie_output<span class="br0">&#40;</span>m<span class="sy0">-&gt;</span>this<span class="sy0">,</span> simpleOutputMethod<span class="sy0">,</span> NULL<span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p>and</p>
<div class="codesnip-container" >
<div class="c codesnip" style="font-family:monospace;">count <span class="sy0">=</span> SWFMovie_save<span class="br0">&#40;</span>m<span class="sy0">-&gt;</span>this<span class="sy0">,</span> STR2CSTR<span class="br0">&#40;</span>s<span class="br0">&#41;</span><span class="sy0">,</span> level<span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p>on line 252 should be replaced with</p>
<div class="codesnip-container" >
<div class="c codesnip" style="font-family:monospace;">Ming_setSWFCompression<span class="br0">&#40;</span>level<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp;count <span class="sy0">=</span> SWFMovie_save<span class="br0">&#40;</span>m<span class="sy0">-&gt;</span>this<span class="sy0">,</span> STR2CSTR<span class="br0">&#40;</span>s<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p>After making these two changes you should be able to follow the normal installation instructions and run the examples included in the source distribution.</p>
<p>Tags: <a href="http://technorati.com/tag/ruby" rel="tag">ruby</a>, <a href="http://technorati.com/tag/flash" rel="tag"> flash</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/ruby/62/building-the-ming-ruby-extension/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fun with ANSI escape codes</title>
		<link>http://www.ioncannon.net/ruby/101/fun-with-ansi-escape-codes/</link>
		<comments>http://www.ioncannon.net/ruby/101/fun-with-ansi-escape-codes/#comments</comments>
		<pubDate>Fri, 03 Nov 2006 13:21:55 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/ruby/101/fun-with-ansi-escape-codes/</guid>
		<description><![CDATA[For fun I recently pimped out a ruby script that I had written for some testing with a little color and a spinner. If you have never used ANSI escape codes before I&#039;ve put together a simple script that shows how easy it is. First let me say that if you are using ruby and [...]]]></description>
			<content:encoded><![CDATA[<p>For fun I recently pimped out a ruby script that I had written for some testing with a little color and a spinner. If you have never used ANSI escape codes before I&#039;ve put together a simple script that shows how easy it is.</p>
<p><span id="more-101"></span></p>
<p>First let me say that if you are using ruby and all you want to do is add color there is a <a href="http://term-ansicolor.rubyforge.org/">Term::ANSIColor gem</a> that you can install. It seems a little overkill to have a gem for this but I figured I would mention it.</p>
<p>The following script displays some sample colors:</p>
<div class="codesnip-container" >
<div class="ruby codesnip" style="font-family:monospace;"><span class="co1">#!/usr/bin/ruby</span></p>
<p><span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[0mAll attributes off<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[1mBold<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[4mUnderline<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[5mBlink<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[8mHide<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[30mBlack<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[31mRed<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[32mGreen<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[33mYellow<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[34mBlue<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[35mMagenta<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[36mCyan<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[37mWhite<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[40m<span class="es0">\0</span>33[37mBlack Background<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[41mRead Background<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[42mGreen Background<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[43mYellow Background<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[44mBlue Background<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[45mMagenta Background<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[46mCyan Background<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[47mWhite Background<span class="es0">\0</span>33[0m<span class="es0">\n</span>&quot;</span></div>
</div>
<p>The only trick to this is that you use an escape code of "\033[" then some code that changes or resets the color. For more information see the Wikipedia link at the end of this post.</p>
<p>And here is how to make a simple spinner:</p>
<div class="codesnip-container" >
<div class="ruby codesnip" style="font-family:monospace;"><span class="co1">#!/usr/bin/ruby</span></p>
<p>sym = <span class="br0">&#91;</span><span class="st0">'|'</span>, <span class="st0">'/'</span>, <span class="st0">'-'</span>, <span class="st0">'<span class="es0">\\</span>'</span><span class="br0">&#93;</span><br />
count = 0</p>
<p><span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[s&quot;</span><br />
10.<span class="me1">times</span> <span class="kw1">do</span><br />
&nbsp; <span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[u<span class="es0">\0</span>33[1D&quot;</span><br />
&nbsp; <span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[34m&quot;</span> <span class="sy0">+</span> sym<span class="br0">&#91;</span>count <span class="sy0">%</span> sym.<span class="me1">length</span><span class="br0">&#93;</span> <span class="sy0">+</span> <span class="st0">&quot;<span class="es0">\0</span>33[0m&quot;</span><br />
&nbsp; <span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[s<span class="es0">\0</span>33[0;0f&quot;</span><br />
&nbsp; STDOUT.<span class="me1">flush</span><br />
&nbsp; <span class="kw3">sleep</span> 1<br />
&nbsp; count <span class="sy0">+</span>= 1<br />
<span class="kw1">end</span><br />
<span class="kw3">printf</span> <span class="st0">&quot;<span class="es0">\0</span>33[u&quot;</span></div>
</div>
<p>If you want the full list of the codes you can use and more general information check out the Wikipedia <a href="http://en.wikipedia.org/wiki/ANSI_escape_code">ANSI Escape Code Information</a>.</p>
<p>Tags: <a href="http://technorati.com/tag/ruby" rel="tag">ruby</a>, <a href="http://technorati.com/tag/ansi" rel="tag"> ansi</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/ruby/101/fun-with-ansi-escape-codes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Credit card type and luhn check in ruby</title>
		<link>http://www.ioncannon.net/ruby/42/credit-card-type-and-luhn-check-in-ruby/</link>
		<comments>http://www.ioncannon.net/ruby/42/credit-card-type-and-luhn-check-in-ruby/#comments</comments>
		<pubDate>Wed, 15 Mar 2006 18:06:47 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/uncategorized/42/credit-card-type-and-luhn-check-in-ruby/</guid>
		<description><![CDATA[I was looking at implementing a luhn and credit card type check the other day in java and I noticed that there seems to be a lack of code for doing this in ruby. So I figured I would put something together for doing the checks in ruby. The following function will do a luhn [...]]]></description>
			<content:encoded><![CDATA[<p>
I was looking at implementing a luhn and credit card type check the other day in java and I noticed that there seems to be a lack of code for doing this in ruby. So I figured I would put something together for doing the checks in ruby.
</p>
<p>
The following function will do a luhn check for a given number (any number not just credit card numbers).  The luhn algorithm is fairly simple, if you want to learn more about it <a href="http://en.wikipedia.org/wiki/Luhn_formula">check here</a>.
</p>
<p><span id="more-42"></span></p>
<div class="codesnip-container" >
<div class="ruby codesnip" style="font-family:monospace;"><span class="kw1">def</span> luhnCheck<span class="br0">&#40;</span>ccNumber<span class="br0">&#41;</span><br />
&nbsp; ccNumber = ccNumber.<span class="kw3">gsub</span><span class="br0">&#40;</span><span class="sy0">/</span>D<span class="sy0">/</span>, <span class="st0">&#034;</span><span class="br0">&#41;</span><br />
&nbsp; cardLength = ccNumber.<span class="me1">length</span><br />
&nbsp; parity = cardLength <span class="sy0">%</span> 2</p>
<p>&nbsp; sum = 0<br />
&nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> 0&#8230;<span class="me1">cardLength</span><br />
&nbsp; &nbsp; digit = ccNumber<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="sy0">-</span> 48</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span> i <span class="sy0">%</span> 2 == parity<br />
&nbsp; &nbsp; &nbsp; digit = digit <span class="sy0">*</span> 2<br />
&nbsp; &nbsp; <span class="kw1">end</span></p>
<p>&nbsp; &nbsp; <span class="kw1">if</span> digit <span class="sy0">&amp;</span>gt; 9<br />
&nbsp; &nbsp; &nbsp; digit = digit <span class="sy0">-</span> 9<br />
&nbsp; &nbsp; <span class="kw1">end</span></p>
<p>&nbsp; &nbsp; sum = sum <span class="sy0">+</span> digit<br />
&nbsp; <span class="kw1">end</span></p>
<p>&nbsp; <span class="kw2">return</span> <span class="br0">&#40;</span>sum <span class="sy0">%</span> 10<span class="br0">&#41;</span> == 0<br />
<span class="kw1">end</span></div>
</div>
<p>
Before running the luhn check you may want to verify that you have a valid card type or at least one you want to accept. The following function will do that based on the current bin ranges for the differenct companies as of today (for more on this see the following: <a href="http://en.wikipedia.org/wiki/Credit_card_number">credit card number information</a> and <a href="http://en.wikipedia.org/wiki/Bank_Identification_Number">BIN range information</a>). N.B. Bin ranges change from time to time so this will become dated. It should be easy enough to find the updated ranges.
</p>
<div class="codesnip-container" >
<div class="ruby codesnip" style="font-family:monospace;"><span class="kw1">def</span> ccTypeCheck<span class="br0">&#40;</span>ccNumber<span class="br0">&#41;</span><br />
&nbsp; ccNumber = ccNumber.<span class="kw3">gsub</span><span class="br0">&#40;</span><span class="sy0">/</span>D<span class="sy0">/</span>, <span class="st0">&#034;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="kw1">case</span> ccNumber<br />
&nbsp; &nbsp; <span class="kw1">when</span> <span class="sy0">/</span>^3<span class="br0">&#91;</span>47<span class="br0">&#93;</span>d<span class="br0">&#123;</span>13<span class="br0">&#125;</span>$<span class="sy0">/</span> <span class="kw1">then</span> <span class="kw2">return</span> <span class="st0">&quot;AMEX&quot;</span><br />
&nbsp; &nbsp; <span class="kw1">when</span> <span class="sy0">/</span>^4d<span class="br0">&#123;</span>12<span class="br0">&#125;</span><span class="br0">&#40;</span>d<span class="br0">&#123;</span>3<span class="br0">&#125;</span><span class="br0">&#41;</span>?$<span class="sy0">/</span> <span class="kw1">then</span> <span class="kw2">return</span> <span class="st0">&quot;VISA&quot;</span><br />
&nbsp; &nbsp; <span class="kw1">when</span> <span class="sy0">/</span>^5d<span class="br0">&#123;</span>15<span class="br0">&#125;</span><span class="sy0">|</span>36d<span class="br0">&#123;</span>14<span class="br0">&#125;</span>$<span class="sy0">/</span> <span class="kw1">then</span> <span class="kw2">return</span> <span class="st0">&quot;MC&quot;</span><br />
&nbsp; &nbsp; <span class="kw1">when</span> <span class="sy0">/</span>^6011d<span class="br0">&#123;</span>12<span class="br0">&#125;</span><span class="sy0">|</span>650d<span class="br0">&#123;</span>13<span class="br0">&#125;</span>$<span class="sy0">/</span> <span class="kw1">then</span> <span class="kw2">return</span> <span class="st0">&quot;DISC&quot;</span><br />
&nbsp; &nbsp; <span class="kw1">when</span> <span class="sy0">/</span>^3<span class="br0">&#40;</span>0<span class="br0">&#91;</span>0<span class="sy0">-</span>5<span class="br0">&#93;</span><span class="sy0">|</span>8<span class="br0">&#91;</span>0<span class="sy0">-</span>1<span class="br0">&#93;</span><span class="br0">&#41;</span>d<span class="br0">&#123;</span>11<span class="br0">&#125;</span>$<span class="sy0">/</span> <span class="kw1">then</span> <span class="kw2">return</span> <span class="st0">&quot;DINERS&quot;</span><br />
&nbsp; &nbsp; <span class="kw1">when</span> <span class="sy0">/</span>^<span class="br0">&#40;</span>39d<span class="br0">&#123;</span>12<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">|</span><span class="br0">&#40;</span>389d<span class="br0">&#123;</span>11<span class="br0">&#125;</span><span class="br0">&#41;</span>$<span class="sy0">/</span> <span class="kw1">then</span> <span class="kw2">return</span> <span class="st0">&quot;CB&quot;</span><br />
&nbsp; &nbsp; <span class="kw1">when</span> <span class="sy0">/</span>^3d<span class="br0">&#123;</span>15<span class="br0">&#125;</span><span class="sy0">|</span>1800d<span class="br0">&#123;</span>11<span class="br0">&#125;</span><span class="sy0">|</span>2131d<span class="br0">&#123;</span>11<span class="br0">&#125;</span>$<span class="sy0">/</span> <span class="kw1">then</span> <span class="kw2">return</span> <span class="st0">&quot;JCB&quot;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span> <span class="kw2">return</span> <span class="st0">&quot;NA&quot;</span><br />
&nbsp; <span class="kw1">end</span><br />
<span class="kw1">end</span></div>
</div>
<p>Tags: <a href="http://technorati.com/tag/ruby" rel="tag">ruby</a>, <a href="http://technorati.com/tag/luhn" rel="tag"> luhn</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/ruby/42/credit-card-type-and-luhn-check-in-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

