- Did duplicate_count.rb
- Learnt .chars actually splits a string into an array of separate characters. But this is case sensitive.
- To transform an array of uppercase and lowercase letters to lowercase
.select{|item| item.respond_to? :downcase}.map(&:downcase)
: before downcase is to see if downcase method exist.
- respond_to? is to see if a method exists within a class.
https://stackoverflow.com/questions/17893977/confused-about-respond-to-method
https://stackoverflow.com/questions/6849722/confused-about-respond-to-vs-respond-to
- .downcase works on a string but not array.
- .map(&:downcase) is the same as .map { |keyword| keyword.downcase }
The &: represents a block.
- .group_by groups the hashes in further array depending on condition
(1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
b = ['a','a','b','b']
b.group_by(&:itself) => {"a"=>["a", "a"], "b"=>["b", "b"]}
b.group_by{|x| x} => {"a"=>["a", "a"], "b"=>["b", "b"]}
- ('a'..'z').count('a') => 1
Basically this counts a from an array of a to z.
- .split(String), .split(Regex), .split(limit)
"sdfgs".split(//)
=> ["s", "d", "f", "g", "s"]
"sdfgs".split('')
=> ["s", "d", "f", "g", "s"]
split btwn each , and the last object is 4 characters.
"1,2,,3,4,,".split(',' , 4) #=> ["1", "2", "", "3,4,,"]
- Remember to note that delete_at(index) returns the value that is deleted.
array.insert(2, array.delete_at(7))
This basically says delete the obj at index 7 and bring it to 2.
- Learnt how to swap posoitions in an array
array = [4, 5, 6, 7]
array[0], array[3] = array[3], array[0]
array # => [7, 5, 6, 4]
- Learnt that .scan seperates a string by regex or a string
a = "cruel world"
a.scan(/\w+/) #=> ["cruel", "world"]
a.scan(/.../) #=> ["cru", "el ", "wor"]
a.scan(/(...)/) #=> [["cru"], ["el "], ["wor"]]
a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]
a.scan(/\w+/) {|w| print "<<#{w}>> " }
print "\n" #=> <<cruel>> <<world>>
a.scan(/(.)(.)/) {|x,y| print y, x }
print "\n" #=> rceu lowlr
- Learnt .chars actually splits a string into an array of separate characters. But this is case sensitive.
- To transform an array of uppercase and lowercase letters to lowercase
.select{|item| item.respond_to? :downcase}.map(&:downcase)
: before downcase is to see if downcase method exist.
- respond_to? is to see if a method exists within a class.
https://stackoverflow.com/questions/17893977/confused-about-respond-to-method
https://stackoverflow.com/questions/6849722/confused-about-respond-to-vs-respond-to
- .downcase works on a string but not array.
- .map(&:downcase) is the same as .map { |keyword| keyword.downcase }
The &: represents a block.
- .group_by groups the hashes in further array depending on condition
(1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
b = ['a','a','b','b']
b.group_by(&:itself) => {"a"=>["a", "a"], "b"=>["b", "b"]}
b.group_by{|x| x} => {"a"=>["a", "a"], "b"=>["b", "b"]}
- ('a'..'z').count('a') => 1
Basically this counts a from an array of a to z.
- .split(String), .split(Regex), .split(limit)
"sdfgs".split(//)
=> ["s", "d", "f", "g", "s"]
"sdfgs".split('')
=> ["s", "d", "f", "g", "s"]
split btwn each , and the last object is 4 characters.
"1,2,,3,4,,".split(',' , 4) #=> ["1", "2", "", "3,4,,"]
- Remember to note that delete_at(index) returns the value that is deleted.
array.insert(2, array.delete_at(7))
This basically says delete the obj at index 7 and bring it to 2.
- Learnt how to swap posoitions in an array
array = [4, 5, 6, 7]
array[0], array[3] = array[3], array[0]
array # => [7, 5, 6, 4]
- Learnt that .scan seperates a string by regex or a string
a = "cruel world"
a.scan(/\w+/) #=> ["cruel", "world"]
a.scan(/.../) #=> ["cru", "el ", "wor"]
a.scan(/(...)/) #=> [["cru"], ["el "], ["wor"]]
a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]
a.scan(/\w+/) {|w| print "<<#{w}>> " }
print "\n" #=> <<cruel>> <<world>>
a.scan(/(.)(.)/) {|x,y| print y, x }
print "\n" #=> rceu lowlr
Comments
Post a Comment