Hanachan in Ruby (non-destructive array manipulation)

listmonster.png

Source: Learn You a Haskell for Great Good!

head

array.first

irb(main):005:0> a = [1, 2, 3]
=> [1, 2, 3]
irb(main):007:0> a.first
=> 1
irb(main):008:0> a
=> [1, 2, 3]

tail

array.drop(1)

irb(main):005:0> a = [1, 2, 3]
=> [1, 2, 3]
irb(main):009:0> a.drop(1)
=> [2, 3]
irb(main):010:0> a
=> [1, 2, 3]

init

array[0...-1]

irb(main):010:0> a
=> [1, 2, 3]
irb(main):011:0> a[0...-1]
=> [1, 2]
irb(main):012:0> a
=> [1, 2, 3]

last

array.last

irb(main):013:0> a
=> [1, 2, 3]
irb(main):014:0> a.last
=> 3
irb(main):015:0> a
=> [1, 2, 3]

Recommended Posts

Hanachan in Ruby (non-destructive array manipulation)
Ruby array manipulation
Multiplication in a Ruby array
Sorting hashes in a Ruby array
[Ruby] Array
[Ruby] Extracting double hash in array
JavaScript array manipulation
Class in Ruby
Ruby two-dimensional array
Heavy in Ruby! ??
About eval in Ruby
Examine the elements in the array using the [Ruby] includes? Method
Calculate the difference between numbers in a Ruby array
Output triangle in Ruby
Date manipulation in Java 8
Variable type in ruby
Fast popcount in Ruby
Multidimensional array in Swift
[Ruby] Count an even number in an array using the even? Method
How to change a string in an array to a number in Ruby
How to retrieve the hash value in an array in Ruby
[Ruby] How to batch convert strings in an array to numbers
ABC177 --solving E in Ruby
Validate JWT token in Ruby
[Ruby] Extracting a two-dimensional array
Read design patterns in Ruby
Write class inheritance in Ruby
Update Ruby in Unicorn environment
Integer unified into Integer in Ruby 2.4
[Ruby] Exception handling in functions
Use ruby variables in javascript.
About regular expressions in Ruby
Three Bit Manipulation Methods (Ruby)
Birthday attack calculation in Ruby
Judgment of fractions in Ruby
[Ruby] Get array elements alternately.
Find Roman numerals in Ruby
Try using gRPC in Ruby
[Ruby] Find numbers in arrays
NCk mod p in Ruby
Chinese Remainder Theorem in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array