Create 9 by 9 Array
string = ["E", "Y", "T", "B", "H", "I", "S", "S", "R", "P", "N", "E", "H", "F", "Qu"]
new_array = Array.new(9) {string.shift(9)}
> [["E", "Y", "T", "B"], ["H", "I", "S", "S"], ["R", "P", "N", "E"], ["H", "F", "Qu"]]
Print Nicely
new_array.map{|r| r.join(" ")}.join "\n"
E Y T B
H I S S
R P N E
H F Qu
Array Arithmetic
array = [["bananna"]*4]*4
> [ ["bananna", "bananna", "bananna", "bananna"],
["bananna", "bananna", "bananna", "bananna"],
["bananna", "bananna", "bananna", "bananna"],
["bananna", "bananna", "bananna", "bananna"]
]
find_all
array = [1,2,3,4,5,5,6,7,7,8]
array.find_all { |x| x % 2 != 0 }
> [1, 3, 5, 5, 7, 7]
map
array = [1, 2, 3, 4, 5]
array.map { |x| x * 2 } => [2, 4, 6, 8, 10]
hash = { :a => 1, :b => 2, :c => 3, :d => 4, :e => 5}
hash.map { |key,value| value * 5} => { :a => 1, :b => 2, :c => 3, :d => 4, :e => 5}
chr
65.chr
> "A"
open (parsing)
open("hello-world.txt", 'w')
Regex: URL Validation
string =~ /https?:\/\/.+\.\D{2,4}/ ? true : false
reject
Takes out elements that are empty spaces or integers
sorted_words.reject! { |c| c.strip.empty? || c =~ /^[-+]?[0-9]+$/}