Last active
April 21, 2019 11:57
-
-
Save dcts/393b4548f0a4b02d0b6e3b59de18ba2e to your computer and use it in GitHub Desktop.
RUBY MAGIC TRICKS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# USEFULL STUFF | |
# do something n times without index | |
10.times { puts "hello" } | |
# n times block with index | |
-5.upto(5) { |indx| puts indx } | |
# iterate over array with n elements of array | |
# will decrease the number of iterations by (n-1) | |
n = 2 | |
arr = [1, 2, 3, 4, 5] | |
arr.each_cons(n) { |a, b| puts "a:#{a}, b:#{b} availible on same iteration" } | |
# transform type of each array element | |
arr = ["1", "2", "3", "4", "5"] | |
arr.map(&:to_i) # [1,2,3,4,5] | |
# sum (or other operator) all elments of number array | |
x = [1, 2, 3, 4] | |
puts x.reduce(:+) # 10 | |
puts x.reduce(:-) # -8 | |
puts x.reduce(:*) # 24 | |
puts x.map(&:to_f).reduce(:/) # 0.04166666 | |
# sum digits of a number in one line | |
puts 1231.to_s.chars.map(&:to_i).reduce(:+) # 7 | |
# chain any iterator (like "map" or "select") with "with_index" | |
arr = ["hello", "wolrd", "ahep", "asd"] | |
arr.map.with_index { |item, indx| "#{item}:#{indx}" } # ["hello:0", "wolrd:1", "ahep:2", "asd:3"] | |
# split array into array of arrays | |
arr = [1, 2, 3, 4] | |
p arr.each_slice(2).to_a # [ [1, 2], [3, 4] ] | |
p arr.each_slice(3).to_a # [ [1, 2, 3], [4] ] | |
# merge neasted arrays with flatten | |
p [[1, 2, 3], [4]].flatten # [ 1, 2, 3, 4 ] | |
p [[[1, 2], 3], [4]].flatten # [ 1, 2, 3, 4 ] | |
# iterate over array of arrays (and name them) | |
[["Alan", "Holmes", 28], ["Max", "Muster", 18]].map { |first, last, age| puts "#{last}, #{first}: #{age}" } | |
# make an array of hashes out of 2 arrays | |
names = ["Holmes", "Thomas", "David"] | |
ages = [12, 25, 29] | |
p names.zip(ages).map { |person| {name: person[0], age: person[1]} } # => [{name: "Holmes, age: 12}, {...}, {...}] | |
# array has duplicates? | |
arr = [1, 2, 3, 4, 4, 5] | |
p arr.uniq.length == arr.length # true | |
# swaping values in one line # be aware: RUBOCOP DOESN'T LIKE IT... | |
arr = ["world", "hello", "!"] | |
arr[1], arr[0] = arr[0], arr[1] | |
p arr | |
# map result of shortcut condition to array | |
p (1..10).map(&:even?) | |
p (1..10).map(&:odd?) | |
# can be done with any function! | |
p ["asd", "12eFd"].map(&:upcase) # ["ASD", "12EFD"] -> applis "capitalize" on all elements of array | |
p ["asd", "12eFd"].map(&:length) # [3, 5] -> returns array with length of each element (as array) | |
# can be even chained | |
p ["olleh", "dlrow"].map(&:upcase).map(&:reverse) # ["HELLO", "WORLD"] -> upcases each element and then reverses each element | |
# ARRAYS | |
a = [1, 2, 3, 4, 5] | |
a.last # => 5 | |
a.first # => 1 | |
a.first(3) # => [1, 2, 3] | |
a.last(3) # => [3, 4, 5] | |
a.take(3) # => [1, 2, 3] -> same as first(n) but doesnt work without argument | |
a.drop(2) # => [3, 4, 5] -> drops first 2 elements and returns the rest | |
# STRINGS | |
str = "Hello World" | |
str.chars.first(5).join # => "Hello" -> take first 5 char of string in one line without using [] | |
str.chars.last(5).join # => "World" -> take last 5 char of string in one line without using [] | |
# different approach (shorter but not so good readible) | |
str[0,5] # => "Hello" -> same as above! -> starts at index 0 and takes 5 chars | |
str[-5,5] # => "World" -> same as above! takes last 5 chars | |
# -------------------- | |
# pause thread (sleep) | |
p "pausing a sec..." | |
10.times do | |
sleep(0.125) | |
p "pausing another sec..." | |
end | |
p "waking up..." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment