9/28/2023 0 Comments Javascript sliceIf we are dealing with arrays in Ruby, we can leverage the #drop method which will behave just like JavaScript’s slice method with a single argument.drop(3) #=> Str 'elephant' #=> 'p' 'elephant' #=> 'phant' Ruby’s #drop method To emulate JavaScript’s way of slicing in Ruby, we can do the following: Str.slice( the_one_index_from_where_to_get_that_one_character_from) 'elephant' #=> 'p' 'elephant'.slice(3) #=> 'p' Str.slice( startingIndexToGetRestOfTheStringFrom) 'elephant' // 'p' 'elephant'.slice(3) // 'phant' Ruby’s slice syntax with one argument: Str 'elephant' #=> 'p' 'elephant' #=> 'n' 'elephant' #=> 'pha' JavaScript’s slice syntax with one argument: To emulate JavaScript’s way of specifying a beginning and an ending slicing indices, we can do the following: Str.slice(starting_index, length) 'elephant' #=> 'p' 'elephant'.slice(3, 3) #=> 'pha' Str.slice(startingIndex, nonInclusiveEndingIndex) 'elephant' // 'p' 'elephant' // 'n' 'elephant'.slice(3, 6) // 'pha' Ruby’s slice syntax with two arguments: Source: JavaScript’s slice syntax with two arguments:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |