When learning a programming language as natural as Ruby, the syntax inevitably seeps into our thoughts and alters the way we conceptualize everything from the complex to the mundane. Thus, the title of this blog (and, indeed, this post).
It is easy for this to happen, in part because Ruby has such an extensive library of methods whose names and behaviors make them ideal for modeling real life. One such method is
slice can be called on an array (or an object that acts like an array, like a string). It literally returns a “slice” of the object on which it is called. The original object is left intact.
> cheese = ['gouda', 'muenster', 'provolone', 'manchego', 'brie'] > my_slices = cheese.slice(1, 2) => ["muenster", "provolone"] > cheese => ["gouda", "muenster", "provolone", "manchego", "brie"]
If you need to take more drastic measures,
slice! will modify the original object, removing (and returning) everything you sliced out.
> cheese = ['gouda', 'muenster', 'provolone', 'manchego', 'brie'] > my_slices = cheese.slice!(1, 2) => ["muenster", "provolone"] > cheese => ["gouda", "manchego", "brie"]
Here are three different ways to use
slice! can also be invoked in these ways, and both can be used with arrays and strings):
array.slice(index)returns the object at
nilif there is no object there to return.
array.slice(start, length)returns a new array containing the elements of
startand continuing for
lengthelements (as shown in the code examples above). If there are no objects there, it returns
array.slice(range)returns a new array containing the objects at
For more on this and other Ruby Array methods, see the documentation.