Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>String#slice!</code> and <code>String#insert</code> is going to get you much closer to what you want without converting your strings to arrays. </p> <p>For example, to simulate <code>Array#pop</code> you can do:</p> <pre><code>text = '¡Exclamation!' mark = text.slice! -1 mark == '!' #=&gt; true text #=&gt; "¡Exclamation" </code></pre> <p>Likewise, for <code>Array#shift</code>:</p> <pre><code>text = "¡Exclamation!" inverted_mark = text.slice! 0 inverted_mark == '¡' #=&gt; true text #=&gt; "Exclamation!" </code></pre> <p>Naturally, to do an <code>Array#push</code> you just use one of the concatenation methods:</p> <pre><code>text = 'Hello' text &lt;&lt; '!' #=&gt; "Hello!" text.concat '!' #=&gt; "Hello!!" </code></pre> <p>To simulate <code>Array#unshift</code> you use <code>String#insert</code> instead, it's a lot like the inverse of slice really:</p> <pre><code>text = 'World!' text.insert 0, 'Hello, ' #=&gt; "Hello, World!" </code></pre> <p>You can also grab chunks from the middle of a string in multiple ways with slice.</p> <p>First you can pass a start position and length:</p> <pre><code>text = 'Something!' thing = text.slice 4, 5 </code></pre> <p>And you can also pass a Range object to grab absolute positions:</p> <pre><code>text = 'This is only a test.' only = text.slice (8..11) </code></pre> <p>In Ruby 1.9 using <code>String#slice</code> like this is identical to <code>String#[]</code>, but if you use the bang method <code>String#slice!</code> it will actually remove the substring you specify.</p> <pre><code>text = 'This is only a test.' only = text.slice! (8..12) text == 'This is a test.' #=&gt; true </code></pre> <p>Here's a slightly more complex example where we reimplement a simple version of <code>String#gsub!</code> to do a search and replace:</p> <pre><code>text = 'This is only a test.' search = 'only' replace = 'not' index = text =~ /#{search}/ text.slice! index, search.length text.insert index, replace text == 'This is not a test.' #=&gt; true </code></pre> <p>Of course 99.999% of the time, you're going to want to use the aforementioned <code>String.gsub!</code> which will do the exact same thing:</p> <pre><code>text = 'This is only a test.' text.gsub! 'only', 'not' text == 'This is not a test.' #=&gt; true </code></pre> <p>references:</p> <ul> <li><a href="http://rubydoc.info/stdlib/core/String" rel="nofollow">Ruby String Documentation</a></li> </ul>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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