Files
lespiedsdanslecode/content/2009/2009-01-18-ruby-if-unless.html
T

125 lines
5.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Ruby if unless - lespiedsdanslecode.org</title>
<meta name="DC.title" content="lespiedsdanslecode.org, site de Yannick François aka yaf (ou ya_f)."/>
<meta name="description" content="Le site de Yannick François. Développeur Senior, Responsable Produit, Pédagogue. Je travail chez Pix"/>
<meta name="keywords" content="code, développement, programmation, apprentissage, apprendre, lean, agile, logiciel, tdd, objet, refactoring, libre, creative commons, linux, unix"/>
<meta name="author" content="Yannick François, https://lespiedsdanslecode.org"/>
<meta name="designer" content="Yannick François, https://lespiedsdanslecode.org"/>
<meta name="geo.placename" content="Le Faouët, Pontivy, Morbihan, Bretagne, France métropolitaine, 56320, 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 / Développeur / Pédagogue" href="index.html" />
<link rel="alternate" type="application/rss" href="/feed.xml" title="lespiedsdanslecode.org/feed" />
<style>
body {
font-size:16pt;
max-width:42rem;
min-width: 18rem;
margin: 1em;
color: black;
background: white;
font-family: Palatino, "Palatino Linotype", "Palatino LT STD", "Book Antiqua", Georgia, serif;
}
a { color: darkred; }
a:hover { background: gold; }
a:visited, a:active { color: dimgrey; }
nav { text-align:center; font-size:smaller; padding: 0; margin: 0; }
nav li { list-style-type: none; }
h2 > a, h2 > a:active, h2 > a:visited { color: lightgrey; text-decoration:none;}
h2 > a:hover { color: black; text-decoration:underline }
@media (max-width: 1000px) { #portrait {display:none;} }
</style>
</head>
<body>
<article>
<h1>Ruby if unless</h1>
<p>Une des choses très agréable avec <a
href="http://ruby-lang.org">Ruby</a> cest ça syntaxe. Un des mots
bien particulier en Ruby est <code>unless</code>.</p>
<p>Je partage tout à fait lavis 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> cest bien,
très bien même dans certain cas, mais en abuser cest 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"=&gt;"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"=&gt;"html"
end
{% endhighlight %}
</pre>
<p>Voir</p>
<pre>
{% highlight ruby %}
xml.subtitle this_blog.blog_subtitle, "type"=&gt;"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? &amp;&amp; settings.has_key?(:url) &amp;&amp; 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 nexprime
pas assez clairement lintention.</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à cest plus clair, on comprend mieux que si
lune des trois conditions nest 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>
</body>
</html>