<?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>MoMolog &#187; Ruby</title>
	<atom:link href="http://momolog.info/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://momolog.info</link>
	<description>MoMolog aus Berlin stellt sich vor. Projekte, Ideen, Referenzen.</description>
	<lastBuildDate>Sun, 18 Jul 2010 22:13:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Copying Files between S3 buckets</title>
		<link>http://momolog.info/2010/07/08/copying-files-between-s3-buckets/</link>
		<comments>http://momolog.info/2010/07/08/copying-files-between-s3-buckets/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 10:27:26 +0000</pubDate>
		<dc:creator>aljoscha</dc:creator>
				<category><![CDATA[AWS]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://momolog.info/?p=268</guid>
		<description><![CDATA[Building on this article here is a simple ruby script, that copies files between two buckets of the same S3 account, omitting files already present (by name). This variant adds a list of path prefixes, so you can selectively copy only certain directories of your buckets. Furthermore it copies the original buckets ACLs for each [...]]]></description>
			<content:encoded><![CDATA[<p>Building on <a href="http://www.lakedenman.com/2009/10/27/copying-files-between-s3-buckets.html">this article</a> here is a simple ruby script, that copies files between two buckets of the same S3 account, omitting files already present (by name).<br />
This variant adds a list of path prefixes, so you can selectively copy only certain directories of your buckets.<br />
Furthermore it copies the original buckets ACLs for each key.</p>
<p><pre><code>
require &#039;rubygems&#039;
require &#039;right_aws&#039;

aws_access_key_id&nbsp;&nbsp;&nbsp;&nbsp; = &#039;YOUR AMAZON ACCESS KEY&#039;
aws_secret_access_key = &#039;YOUR AMAZON SECRET ACCESS KEY&#039;
source_bucket&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = &#039;SOURCE BUCKET NAME&#039;
target_bucket&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = &#039;TARGET BUCKET NAME&#039;
prefixes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= [PATH_PREFIX1, PATH_PREFIX2, ...]

s3 = RightAws::S3Interface.new(aws_access_key_id, aws_secret_access_key)

copied_keys = Array.new
(prefixes || [&#039;&#039;]).each do |prefix|
&nbsp;&nbsp;s3.incrementally_list_bucket(target_bucket, {:prefix =&gt; prefix}) do |key_set|
&nbsp;&nbsp;&nbsp;&nbsp;copied_keys &lt;&lt; key_set[:contents].map{|k| k[:key]}.flatten
&nbsp;&nbsp;end
end
copied_keys.flatten!

(prefixes || [&#039;&#039;]).each do |prefix|
&nbsp;&nbsp;s3.incrementally_list_bucket(source_bucket, {:prefix =&gt; prefix}) do |key_set|
&nbsp;&nbsp;&nbsp;&nbsp;key_set[:contents].each do |key|
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key = key[:key]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if copied_keys.include?(key)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts &quot;#{target_bucket} #{key} already exists. Skipping...&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts &quot;Copying #{source_bucket} #{key}, setting acl&quot;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retries=0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s3.copy(source_bucket, key, target_bucket)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acl = s3.get_acl(source_bucket, key)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s3.put_acl(target_bucket, key, acl[:object])
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue Exception =&gt; e
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts &quot;cannot copy key, #{e.inspect}\nretrying #{retries} out of 10 times...&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retries += 1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retry if retries &lt;= 10
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;end
end
</code></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://momolog.info/2010/07/08/copying-files-between-s3-buckets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vor kurzem dazugelernt:</title>
		<link>http://momolog.info/2009/10/18/vor-kurzem-dazugelernt/</link>
		<comments>http://momolog.info/2009/10/18/vor-kurzem-dazugelernt/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 15:46:19 +0000</pubDate>
		<dc:creator>aljoscha</dc:creator>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Note to self]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://momolog.info/?p=204</guid>
		<description><![CDATA[Suche nach Wort unter dem Cursor in vim: #. jssh ist eine JavaScript Shell, die den Firefox per Port 9997 fernsteuerbar macht. Download z.B. hier. y erzeugt einen YAML dump auf der Rails console, mehr dazu hier. =3D ist ein escaptes &#8220;=&#8221; in quoted_printable. sudo /usr/libexec/locate.updatedb aktualisiert unter MacOSX sofort die locate Datenbank. rake db:migrate:redo [...]]]></description>
			<content:encoded><![CDATA[<p>Suche nach Wort unter dem Cursor in vim: <code>#</code>.</p>
<p><code>jssh</code> ist eine JavaScript Shell, die den Firefox per Port 9997 fernsteuerbar macht.<br />
Download z.B. <a href="http://wiki.openqa.org/display/WTR/FireWatir+Installation">hier</a>.</p>
<p><code>y</code> erzeugt einen YAML dump auf der Rails console, mehr dazu <a href="http://blog.floehopper.org/articles/2006/12/22/rails-console-shortcuts">hier</a>.</p>
<p><code>=3D</code> ist ein escaptes &#8220;=&#8221; in <a href="http://de.wikipedia.org/wiki/Quoted-printable">quoted_printable</a>.</p>
<p><code>sudo /usr/libexec/locate.updatedb</code> aktualisiert unter MacOSX sofort die <code>locate</code> Datenbank.</p>
<p><code>rake db:migrate:redo</code> führt unter rails die letzte Migration rückwärts und sofort wieder vorwärts aus, so dass sich die Vorwärts-Action korrigieren läßt </p>
<p><code>ack -Q</code> bringt <a href="http://betterthangrep.com/">ack</a> dazu, <em>literal</em>, also ohne RegExp zu suchen.</p>
]]></content:encoded>
			<wfw:commentRss>http://momolog.info/2009/10/18/vor-kurzem-dazugelernt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What ist this?</title>
		<link>http://momolog.info/2009/05/15/what-ist-this/</link>
		<comments>http://momolog.info/2009/05/15/what-ist-this/#comments</comments>
		<pubDate>Fri, 15 May 2009 17:25:24 +0000</pubDate>
		<dc:creator>aljoscha</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://momolog.info/?p=37</guid>
		<description><![CDATA[Laut PHP-Doku ist das Verhalten der Pseudo-Variablen $this wie folgt: $this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object). Schauen wir uns folgendes Beispiel an: class A{ &#160;&#160;function __construct(){ &#160;&#160;&#160;&#160;$this-&#62;name [...]]]></description>
			<content:encoded><![CDATA[<p>Laut <a href="http://www.php.net/manual/en/language.oop5.basic.php">PHP-Doku</a> ist das Verhalten der Pseudo-Variablen <code>$this</code> wie folgt:</p>
<blockquote><p>$this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object).
</p></blockquote>
<p>Schauen wir uns folgendes Beispiel an:<br />
<pre><pre>
class A{
&nbsp;&nbsp;function __construct(){
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;name = &#039;A&#039;;
&nbsp;&nbsp;}

&nbsp;&nbsp;function echoThisName(){
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;My Name is {$this-&gt;name}.\n&quot;;
&nbsp;&nbsp;}
}
</pre></pre><br />
Jetzt rufen wir die Methode mal als Instanzmethode und mal statisch auf:<br />
<pre><pre>
$a = new A();
$a-&gt;echoThisName();
A::echoThisName();

My Name is A.
PHP Fatal error:&nbsp;&nbsp;Using $this when not in object context in 
/Users/aljoscha/test.php on line 9
Fatal error: Using $this when not in object context in 
/Users/aljoscha/test.php on line 9
</pre></pre><br />
Das ist vernünftig. (ausser: Warum muss sich PHP eigentlich immer wiederholen? Ist eine Fehlermeldung zu subtil?)<br />
Jetzt rufen wird diese Methoden aus einer anderen Klasse <code>B</code> heraus auf:<br />
<pre><pre>
class B{
&nbsp;&nbsp;function __construct(){
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;name = &#039;B&#039;;
&nbsp;&nbsp;}

&nbsp;&nbsp;function echoAsName(){
&nbsp;&nbsp;&nbsp;&nbsp;$a = new A();
&nbsp;&nbsp;&nbsp;&nbsp;$a-&gt;echoThisName();
&nbsp;&nbsp;&nbsp;&nbsp;A::echoThisName();
&nbsp;&nbsp;}
}

$b = new B();
$b-&gt;echoAsName();
</pre></pre><br />
Und das gibt:<br />
<pre><pre>
My Name is A.
My Name is B.
</pre></pre><br />
Wir haben die Methode <code>echoThisName</code> der Klasse <code>A</code> <em>statisch</em> aufgerufen, aber <code>$this</code> ist darin trotzdem gesetzt, und zwar als wären wir in der Instanz <code>$b</code> der Klasse <code>B</code>.<br />
<code>$b</code> hat sich die statische Methode gekapert. </p>
<p>Was sagt Ruby dazu?<br />
<pre><pre>

class A
&nbsp;&nbsp;def initialize
&nbsp;&nbsp;&nbsp;&nbsp;@name = &#039;A&#039;;
&nbsp;&nbsp;end

&nbsp;&nbsp;def echoThisName
&nbsp;&nbsp;&nbsp;&nbsp;puts &quot;My Name is #{@name}.\n&quot;;
&nbsp;&nbsp;end
end

$a = A.new;
$a.echoThisName;
A::echoThisName;
</pre></pre><br />
ergibt:<br />
<pre><pre>

My Name is A.
test.rb:13: undefined method `echoThisName&#039; for A:Class (NoMethodError)
</pre></pre><br />
Rufen wir <code>A::echoThisName</code> aus einer Instanz von <code>B</code> auf:<br />
<pre><pre>

class B
&nbsp;&nbsp;def initialize
&nbsp;&nbsp;&nbsp;&nbsp;@name = &#039;B&#039;;
&nbsp;&nbsp;end

&nbsp;&nbsp;def echoAsName
&nbsp;&nbsp;&nbsp;&nbsp;$a = A.new;
&nbsp;&nbsp;&nbsp;&nbsp;$a.echoThisName;
&nbsp;&nbsp;&nbsp;&nbsp;A::echoThisName;
&nbsp;&nbsp;end
end

$b = B.new;
$b.echoAsName;
</pre></pre><br />
Ist das Ergebnis entsprechend:<br />
<pre><pre>
My Name is A.
test.rb:23:in `echoAsName&#039;: undefined method `echoThisName&#039; 
for A:Class (NoMethodError) from test.rb:28
</pre></pre><br />
Besser.<br />
<i>Dank an Stephan für den Hinweis auf die Dokumentation des Verhaltens in PHP.</i></p>
]]></content:encoded>
			<wfw:commentRss>http://momolog.info/2009/05/15/what-ist-this/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fixing the Prototype Enumeration Mixin</title>
		<link>http://momolog.info/2006/10/14/prototype-enumeration/</link>
		<comments>http://momolog.info/2006/10/14/prototype-enumeration/#comments</comments>
		<pubDate>Sat, 14 Oct 2006 11:42:56 +0000</pubDate>
		<dc:creator>aljoscha</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[The following article describes ticket #3592 in the RoR Trac and explains a proposed solution. Prototypes Enumerable mixin class does not properly respect the mixees internal format. For example the reject and findAll methods, operating on Hashes, return Arrays instead of Hashes. var a = $H({a:1, b:2, c:1, d:3}); document.writeln(a.inspect().escapeHTML()); =&#62;#&#60;Hash:{&#039;a&#039;: 1, &#039;b&#039;: 2, &#039;c&#039;: [...]]]></description>
			<content:encoded><![CDATA[<p>The following article describes <a href="http://dev.rubyonrails.org/ticket/3592">ticket #3592</a> in the RoR Trac and explains a proposed solution.</p>
<p>Prototypes Enumerable mixin class does not properly respect the mixees internal format. For example the <code>reject</code> and <code>findAll</code> methods, operating on Hashes, return Arrays instead of Hashes.<br />
<pre><pre>
var a = $H({a:1, b:2, c:1, d:3}); 
document.writeln(a.inspect().escapeHTML());

=&gt;#&lt;Hash:{&#039;a&#039;: 1, &#039;b&#039;: 2, &#039;c&#039;: 1, &#039;d&#039;: 3}&gt;

var b = a.reject(function(val){ return (val[1]==1) });
document.writeln(b.inspect().escapeHTML());

=&gt; [[&#039;b&#039;, 2], [&#039;d&#039;, 3]]&nbsp;&nbsp; // !!! should be hash

var c = b.findAll(function(val){ return (val[0]==&#039;d&#039;) });
document.writeln(c.inspect().escapeHTML());

=&gt; [[&#039;b&#039;, 2], [&#039;d&#039;, 3]]&nbsp;&nbsp; // !!! should be hash
</pre></pre></p>
<p>In Ruby (from which the inspiration for the Enumerable Mixin stems), the reject method does return a hash, not an array, when operating on an hash:<br />
<pre><pre> 
irb(main):001:0&gt; a={:a =&gt; 1, :b =&gt; 2, :c =&gt; 1, :d =&gt; 3}&nbsp;&nbsp;

=&gt; {:b=&gt;2, :c=&gt;1, :a=&gt;1, :d=&gt;3}&nbsp;&nbsp;

irb(main):002:0&gt; b=a.reject{|k,v| v==1}

=&gt; {:b=&gt;2, :d=&gt;3} 
</pre></pre><br />
The patch requires all classes that want to mixin Enumerable to define two more methods similar to <code>_each</code>:<br />
<code>_new</code>, which returns an empty object of that class and<br />
<code>_add</code>, that adds an element. Enumerable mixin thus needs to make no more assumptions about the internal structure of its mixee class.</p>
<p>After the fix the methods return correctly:<br />
<pre><pre>
=&gt; #&lt;Hash:{&#039;b&#039;: 2, &#039;d&#039;: 3}&gt;
</pre></pre> </p>
]]></content:encoded>
			<wfw:commentRss>http://momolog.info/2006/10/14/prototype-enumeration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
