Last active
February 17, 2016 01:56
-
-
Save codepedia/a7402f02c3d93965ca6e to your computer and use it in GitHub Desktop.
This file contains 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
The below ,removes anything but ascii characters like: | |
variant_type.gsub!(/[^0-9A-Za-z.\-]/, '') | |
sql = "INSERT INTO my_table VALUES(#{rows[i].map{|x| x.inspect}.join(', ')})" | |
vs | |
sql = "INSERT INTO my_table VALUES(#{ ary.map{ |i| '"%s"' % i }.join(', ') })" | |
# => "INSERT INTO my_table VALUES(\"value 1\", \"value 2\", \"value 3\")" | |
puts sql | |
# >> INSERT INTO my_table VALUES("value 1", "value 2", "value 3") | |
'"%s"' % i is a String format that wraps a string in double-quotes. | |
============================= | |
# takes [[4]] and makes it into a 4 "integer" | |
result = result.join().to_i | |
# takes [[4]] and makes it into a 4 "integer".. | |
# array to string , strip th "" , remove th [[]] , converts to integer. | |
recordcount = result.to_s.gsub('"',"").gsub(/[\[\]]/, '').to_i | |
OR like this ... even better: | |
irb(main):001:0> arr = [[4]] | |
=> [[4]] | |
irb(main):002:0> arr.flatten | |
=> [4] | |
irb(main):003:0> arr.flatten[0] | |
or | |
=> 4 | |
irb(main):004:0> arr.flatten.first | |
=> 4 | |
============================= | |
The map method takes an enumerable object and a block, and runs the block for each element, outputting each returned value from the block (the original object is unchanged unless you use map!): | |
[1, 2, 3].map { |n| n * n } #=> [1, 4, 9] | |
Array and Range are enumerable types. map with a block returns an Array. map! mutates the original array. | |
============================= | |
string = "12345678" | |
=> [["12"], ["34"], ["56"], ["78"]] | |
irb(main):042:0> string.scan(/(\d)/) | |
=> [["1"], ["2"], ["3"], ["4"], ["5"], ["6"], ["7"], ["8"]] | |
irb(main):043:0> string.scan(/(..)/) | |
=> [["12"], ["34"], ["56"], ["78"]] | |
irb(main):045:0> string.scan(/(\d{2})/) | |
=> [["12"], ["34"], ["56"], ["78"]] | |
irb(main):046:0> string.scan(/(\d{2})/) | |
=> [["12"], ["34"], ["56"], ["78"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment