String split method is initiated by using the syntax .split at the end of a string object. An array is returned as a result of using the split method. If just .split is used, without parenthesis, it will return the string object in an array with each sub-string object being split out into an individual object inside of the array. The sub-strings are split off based off of the location of whitespace in the original string object. It is possible to modify how the original string is split by providing arguments to the .split.
- For example: .split(//) will genreate an array that has each individual character in the original substring split off from the original string, including whitespace.
- For example: "dog".split(//) will output ["d", "o", "g"].
Array slice method is initiated by using the syntax [] at the end of an array. A string, array, or nil is returned as a result of using the slice method, depending on the argument provided. An number is provided as an argument into the slice method. Multiple numerical arguments can be provided, including a range. If a signle number is provided, it will return an array with the element from the original array the corresponds to the number choosen.
- For example: d = ["d", "o", "g"], d[2] will output
d.
- string split method: ruby-doc.org
- array slice method: ruby-doc.org
Micro Titans
String data:
p1 = "Microscopic"
p2 = "Gigantic"
Integer and/or float data:
hit_points = 50
height_of_mountain = 14.5
Boolean data:
hit = true
miss = false
Array data:
weapons = \["ray gun", "pie", "shrink ray"]
landscapes = \["rivers", "mountains", "fields"]
Hash data:
starting_health = {"Microsopic" => 50, "Gigantic" => 50}