Ajoute les articles manquants
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Yannick François - Les pieds dans le code</title>
|
||||
|
||||
<meta name="DC.title" content="elsif.fr, site de Yannick François aka yaf aka pouype."/>
|
||||
<meta name="description" content="Yannick François's website. Senior developer, Code pedagogist. /ut7 teammate, proud member of Electrolab, the crazy hackerspace. Eternel supporter of April, a french association about freesoftware."/>
|
||||
<meta name="keywords" content="code, développement, lean, agile, logiciel, tdd, objet, ruby, site perso, personnel, ruby on rails, refactoring, openbsd, bsd, libre, creative commons, unix"/>
|
||||
<meta name="author" content="Yannick François, https://elsif.fr"/>
|
||||
<meta name="designer" content="Yannick François, https://elsif.fr"/>
|
||||
<meta name="geo.placename" content="Poissy, Ile de france, France"/>
|
||||
<meta name="robots" content="index,follow" />
|
||||
<meta name="language" content="French" />
|
||||
<meta name="HandheldFriendly" content="True" />
|
||||
<meta name="MobileOptimized" content="320" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
|
||||
<link rel="index" title="Yannick François / Developer" href="https://elsif.fr" />
|
||||
<link rel="stylesheet" href="/css/knacss.css" media="all"/>
|
||||
<link rel="stylesheet" href="/css/elsif.css" media="all"/>
|
||||
|
||||
|
||||
<article>
|
||||
|
||||
<h1>
|
||||
Ruby if unless
|
||||
|
||||
</h1>
|
||||
|
||||
<p>Une des choses très agréable avec <a href="http://ruby-lang.org">Ruby</a> c’est ça syntaxe. Un des mots bien particulier en Ruby est <code>unless</code>.</p>
|
||||
|
||||
<p>Je partage tout à fait l’avis de ce billet <a href="http://railstips.org/2008/12/1/unless-the-abused-ruby-conditional">Unless, The Abused Ruby Conditional</a> <code>unless</code> c’est bien, très bien même dans certain cas, mais en abuser c’est mal. Ce mot peut rendre les choses plus lisible tout comme il pourrait les compliquer.</p>
|
||||
|
||||
<p>Une condition doit représenter une intention, unless permet de le faire, mais cela doit rester une intention.</p>
|
||||
|
||||
<pre>
|
||||
{% highlight ruby %}
|
||||
xml.updated @items.first.updated_at.xmlschema unless @items.empty?
|
||||
{% endhighlight %}
|
||||
</pre>
|
||||
|
||||
<p>Ce code, extrait de la classe feed.atom.builder de <a href="http://typosphere.org">Typo</a> est une bonne utilisation de <code>unless</code>. On évite ainsi le vilain:</p>
|
||||
|
||||
<pre>
|
||||
{% highlight ruby %}
|
||||
xml.updated @items.first.updated_at.xmlschema if !@items.empty?
|
||||
{% endhighlight %}
|
||||
</pre>
|
||||
|
||||
<p>Un peu comme, et dans la même classe, nous avons un peu plus haut:</p>
|
||||
|
||||
<pre>
|
||||
{% highlight ruby %}
|
||||
if(not this_blog.blog_subtitle.blank?)
|
||||
xml.subtitle this_blog.blog_subtitle, "type"=>"html"
|
||||
end
|
||||
{% endhighlight %}
|
||||
</pre>
|
||||
|
||||
<p>Assez étrange, nous aurions pu avoir plutôt</p>
|
||||
|
||||
<pre>
|
||||
{% highlight ruby %}
|
||||
unless this_blog.blog_subtitle.blank?
|
||||
xml.subtitle this_blog.blog_subtitle, "type"=>"html"
|
||||
end
|
||||
{% endhighlight %}
|
||||
</pre>
|
||||
|
||||
<p>Voir</p>
|
||||
|
||||
<pre>
|
||||
{% highlight ruby %}
|
||||
xml.subtitle this_blog.blog_subtitle, "type"=>"html" unless this_blog.blog_subtitle.blank?
|
||||
{% endhighlight %}
|
||||
</pre>
|
||||
|
||||
<p>Il y a part contre dans la méthode <code>ping_article!</code> du modèl <code>blog.rb</code> une mauvaise utilisation de <code>unless</code> (à mon avis)</p>
|
||||
|
||||
<pre>
|
||||
{% highlight ruby %}
|
||||
unless global_pings_enabled? && settings.has_key?(:url) && settings.has_key?(:article_id)
|
||||
throw :error, "Invalid trackback or trackbacks not enabled"
|
||||
end
|
||||
{% endhighlight %}
|
||||
</pre>
|
||||
|
||||
<p>et une bonne.</p>
|
||||
|
||||
<pre>
|
||||
{% highlight ruby %}
|
||||
unless article.allow_pings?
|
||||
throw :error, "Trackback not saved"
|
||||
end
|
||||
{% endhighlight %}
|
||||
</pre>
|
||||
|
||||
<p>Pour la bonne, rien à dire. Par contre, la première n’exprime pas assez clairement l’intention.</p>
|
||||
|
||||
<pre>
|
||||
{% highlight ruby %}
|
||||
if !global_pings_enabled? || !settings.has_key?(:url) || !settings.has_key?(:article_id)
|
||||
throw :error, "Invalid trackback or trackbacks not enabled"
|
||||
end
|
||||
{% endhighlight %}
|
||||
</pre>
|
||||
|
||||
<p>Je trouve que là c’est plus clair, on comprend mieux que si l’une des trois conditions n’est pas rempli, on lève un exception.</p>
|
||||
|
||||
<p><em>Tout ceci est une histoire de gout peut-être, vous en pensez quoi ?</em></p>
|
||||
|
||||
<article>
|
||||
|
||||
|
||||
<footer style="font-size:75%;text-align:right"><em><a href="/feed.xml">suivre les articles</a></em></footer>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user