TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to RUBY

Ruby Copy Array Example

Copy arrays with the slice operator. Changes to a copied array do not affect the original one.
Copy array. An array is stored in a region of memory. When we modify it, we modify that region of memory. Two array variables can point to the same region. We can copy an array, with slice syntax, to create separate arrays.Array
Example. First, this program slices an array with the square-bracket syntax. It specifies a slice on "values" from 0 to values.length. This is a total-array slice. After the slice, "values" and "copy" point to separate memory regions.

Note: We test that our arrays are separate. We modify the copied array, and the original array is not changed.

Ruby program that copies array # Create an array. values = ["cat", "dog", "mouse"] print "VALUES: ", values, "\n" # Copy the array using a total-array slice. # ... Modify the new array. copy = values[0 .. values.length] copy[0] = "snail" print "COPY: ", copy, "\n" # The original array was not modified. print "VALUES: ", values, "\n" Output VALUES: ["cat", "dog", "mouse"] COPY: ["snail", "dog", "mouse"] VALUES: ["cat", "dog", "mouse"]
Example 2. This example is similar, but has one difference. It specifies the length of the slice with a final index of negative one. This means "the last index." This is the shortest syntax form.

Tip: This syntax form is not better (or worse) than the previous one. It comes down to what syntax form you prefer.

Ruby program that copies array with negative one values = [1, 10, 100] # Copy the array with a different syntax. copy = values[0 .. -1] copy[0] = 1000 # Display the arrays. print values, "\n" print copy, "\n" Output [1, 10, 100] [1000, 10, 100]
Slice. The slice method is an alias for the square-bracket slicing syntax. So calling slice with the same arguments as above yields an equivalent result. Please be sure to use two dots to separate arguments, not a comma.

Opinion: For an experienced developer using Ruby, a total-array slice is easy to understand.

Ruby program that uses slice, copies array values = [8, 9, 10] # Use slice method to copy array. copy = values.slice(0 .. -1) copy[0] = 5 # Display two arrays. print values, "\n" print copy, "\n" Output [8, 9, 10] [5, 9, 10]
Summary. When developing programs, an array copy is sometimes needed. If you do not copy an array, the original array will be modified. And once modified, you might not be able to retrieve the original values.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf