Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>why do I get nil when I reverse the order of the words.first.capitalize and words.join(" ") lines?</p> </blockquote> <p>This is the reason:</p> <pre><code>"abb".capitalize # =&gt; "Abb" "Abb".capitalize! # =&gt; nil "Abb".capitalize # =&gt; "Abb" </code></pre> <p><a href="http://www.ruby-doc.org/core-2.0.0/String.html#method-i-capitalize-21" rel="nofollow"><strong><code>String.capitalize!</code></strong></a> says:</p> <blockquote> <p>Modifies str by converting the first character to uppercase and the remainder to lowercase. <strong><em>Returns nil if no changes are made</em></strong>. Note: case conversion is effective only in ASCII region.</p> </blockquote> <p>When you are putting the line <code>words.first.capitalize!</code> at the end of your method, the <code>String.capitalize!</code> tried to capitalize the word which is already capitalized. You are using the <code>bang(!)</code> version, so, as per the documentation, you are getting <code>nil</code>.</p> <p>One example:</p> <pre><code>def titleize(x) words = x.split(" ").collect do |word| if %w(the and of).include?(word) word else word.capitalize end end words.join(" ") words.first.capitalize! end titleize("he great book") # =&gt; nil </code></pre> <p>Probably your input string was not containing any of the words you listed here <code>%w(the and of)</code>, when you were testing. I would recommend you to use <a href="http://www.ruby-doc.org/core-2.0.0/String.html#method-i-capitalize" rel="nofollow"><strong><code>String#capitalize</code></strong></a> instead. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload