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
JavaScript array manipulation
Ruby two-dimensional array
Heavy 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
ABC177 --solving E in Ruby
Validate JWT token in Ruby
[Ruby] Extracting a two-dimensional array
Write class inheritance in Ruby
Update Ruby in Unicorn environment
Integer unified into Integer in Ruby 2.4
[Ruby] Exception handling in functions
About regular expressions in Ruby
NCk mod p in Ruby