
ruby
bluexuemei
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
ruby中的整数、浮点数、字符串之间的相互转换
浮点数转换成整数,会强行去掉小数点后面的数字irb(main):017:0>123.45.to_i=> 123整数转换成浮点数,会添加小数点和0irb(main):018:0>123.to_f=> 123.0转载 2014-05-12 10:17:27 · 522 阅读 · 0 评论 -
Ruby remove substring matching to any element of array
Ruby remove substring matching to any element of array转载 2014-05-17 14:35:20 · 447 阅读 · 0 评论 -
Ruby中将代码块作为参数传入方法的几种方法及其区别
Ruby中将代码块作为参数传入方法的几种方法及其区别这篇文章只是随手写写玩玩的,我对Ruby的Block和Lambda其实理解并不怎么深刻,知识现在够用就差不多了吧。第一种方法最传统了:?123456def f1(a) yieldaend f1(1){|a| a.to_s}# "1"转载 2014-05-12 07:46:19 · 1088 阅读 · 0 评论 -
Ruby对时间的处理函数
Ruby对时间的处理函数1.当前时间 t = Time.new 或 t = Time.now2.生成指定时间 t1 =Time.mktime(2001) # January 1, 2001 at 0:00:00 t2 = Time.mktime(2001,3) t3 = Time.mktime(2001,3,15)转载 2014-05-12 10:30:11 · 520 阅读 · 0 评论 -
ruby正则表达式不支持逆序环视?该怎么解决
ruby正则表达式不支持逆序环视?该怎么解决2012-03-18 来源:读书人网 【读书人网(Reader8.cn):综合教育门户网站】0ruby正则表达式不支持逆序环视?在《精通正则表达式》一书中,作者介绍了如何使用顺序环视和逆序环视在数值中ruby正则表达式不支持逆序环视?在《精通正则表达式》一书中,作者介绍了如何使用顺序环视和逆序环视在数值中插入逗号:转载 2014-05-17 20:44:27 · 601 阅读 · 0 评论 -
列出最近8天的日期
require "date"today = Date.today(today-7..today).map { |date| date.strftime("%m-%d") }转载 2014-05-18 14:16:38 · 394 阅读 · 0 评论 -
Regexp
RegexpRegexp是处理正则表达式的类。正则表达式的字面值是以双斜线内夹表达式的形式生成的。/^this is regexp/还可以使用Regexp.new(string)来动态地生成正则表达式对象。类方法Regexp.compile(string[, option[, code]])Regexp.new(string[, option[, cod转载 2014-05-17 14:59:44 · 735 阅读 · 0 评论 -
interger to IP address
interger to I P addressAaron Gifford astounding at gmail.com Wed Aug 27 17:05:32 CDT 2008Previous message: interger to I P addressNext message: interger to I P addressMessages sorted by:转载 2014-05-11 23:44:56 · 378 阅读 · 0 评论 -
array summation and merging based on uniqueness of element at certain position + ruby arrays
I have a 2D array as below[[1, 4.0, "burger"], [1, 8.0, "tofu_log"], [2, 5.0, "burger"], [2, 6.5, "tofu_log"]]Here, each element is a collection of restaurant_id, price and item. The task is to re转载 2014-05-18 13:34:05 · 3707 阅读 · 0 评论 -
Appending to JSON array in Ruby
I am looking to append to a JSON array in ruby. The JSON array looks like this:{"data" : [{"name":"Chris","long":10,"lat":19}, {"name":"Scott","long":9,"lat":18}]}I want to be able to append anoth转载 2014-06-02 17:38:36 · 737 阅读 · 0 评论 -
Partially sorting an array in Ruby
'm trying to sort an array of arrays alphabetically at the exclusion of the word "Other", which is a member that I want at the end of the array. Let's say that the array looks like the following:cit转载 2014-06-02 15:57:42 · 472 阅读 · 0 评论 -
Zip together sub arrays of a 2 dimensional array
Zip together sub arrays of a 2 dimensional arrayIs there a general way of doing this ?Each sub array will be of the same length.c = [[1,1,1,1], [2,2,2,2], [3,3,3,3]]c[0].zip(c[1], c[2])=转载 2014-06-02 17:12:55 · 607 阅读 · 0 评论 -
利用正则截取字符串
my_array = ["sh.please-word", ".things-to-do" , "#cool-stuff", "span.please-word-not"]转载 2014-05-21 10:39:17 · 425 阅读 · 0 评论 -
Collect index from Array/String Matchdata
If you want to get each character's index as a hash, this would work:b = %w(A B A A C C B D A D)h = {}b.each_with_index { |e, i| h[e] ||= [] h[e] i}h#=> {"A"=>[0, 2, 3, 8], "B"=>[1, 6], "转载 2014-06-02 15:51:24 · 433 阅读 · 0 评论 -
how to merge array of hash based same keys in ruby?
how to merge array of hash based same keys in ruby?example :a = [{:a=>1},{:a=>10},{:b=>8},{:c=>7},{:c=>2}]how to get result like this?a = [{:a=>[1, 10]},{:b=>8},{:c=>[7, 2]}]thanks before转载 2014-06-04 15:17:26 · 458 阅读 · 0 评论 -
How to add two multidimensional array in ruby
I am working in a ruby on rails project where i am creating complex algorithm, i need help regarding adding 2 two dimensional array.i want to do following:array1 = [[1,10],[2,20],[3,10],[4,30]]arr转载 2014-06-04 16:36:12 · 458 阅读 · 0 评论 -
How to call different methods as given in an array
Especially when working with structs, it would be nice to be able to call a different method per each element in an array, something like this:array = %w{name 4 tag 0.343}array.convert(:to_s, :to_i转载 2014-06-23 11:36:52 · 314 阅读 · 0 评论 -
array x array matrix in ruby
I'd like to convert from{'key1' => (1..10) , 'key2' => (11..20) , 'key3' => (21..30)}to[{'key1' => 1, 'key2' => 11, 'key3' => 21},{'key1' => 1, 'key2' => 11, 'key3' => 22},... . .{'key1' =转载 2014-06-23 11:48:03 · 415 阅读 · 0 评论 -
Ruby: Sum selected hash values
I've got an array of hashes and would like to sum up selected values. I know how to sum all of them or one of them but not how to select more than one key.i.e.:[{"a"=>5, "b"=>10, "active"=>"yes"},转载 2014-06-23 12:31:55 · 431 阅读 · 0 评论 -
Array into ranges of consecutive numbers
ar = [1, 2, 3, 5, 6, 8, 9]prev = ar[0]p ar.slice_before { |e| prev, prev2 = e, prev prev2 + 1 != e}.map{|a| a[0]..a[-1]} # >> [1..3, 5..6, 8..9]ar = [1, 2, 3, 5, 6,7, 8, 9,11]prev = ar[0]p转载 2014-06-23 12:54:18 · 355 阅读 · 0 评论 -
Condensing an array of hashes within that same array in Ruby
Basically I'm trying to condense this one array of hashes with the same value into another array. I'm new to Ruby and I'm trying to change thisfruit = [ {type: 'grape', color: 'purple' }, {type: '转载 2014-06-23 13:04:59 · 334 阅读 · 0 评论 -
Merge array and hash in ruby if key appears in array
I have two arrays one = [1,2,3,4,5,6,7] and two = [{1=>'10'},{3=>'22'},{7=>'40'}]Two will have one.length hashes or less. I want a new array of values from two if it's key appears in one, if not the转载 2014-06-04 12:15:17 · 810 阅读 · 0 评论 -
Creating this array programmatically?
(0..12).map { |i| "rate_limit_%03d" % i }# => ["rate_limit_000", "rate_limit_001", "rate_limit_002", ...转载 2014-06-04 16:20:04 · 314 阅读 · 0 评论 -
Stack Overflow Questions Tags Users Badges Unanswered Ask Question ruby
["ABC", "DEF", "GHI"]should generate["ADG", "ADH", "ADI", "AEG", "AEH", "AEI", "AFG", "AFH", "AFI", "BDG", "BDH", "BDI", "BEG", "BEH", "BEI", "BFG", "BFH", "BFI", "CDG", "CDH", "CDI", "CEG", "CE转载 2014-06-03 19:44:28 · 17771 阅读 · 0 评论 -
格式化字符串
def createPhoneNumber(numbers) "(#{numbers[0..2].join}) #{numbers[3..5].join}-#{numbers[6..9].join}"end转载 2014-06-04 16:55:13 · 357 阅读 · 0 评论 -
Replace every item in an array with an empty item
For instance, if I have this array: [ 0, 5, 4, 7, 1 ]I need to change it to: [ '', '', '', '', '']Enumerable#map replaces every element with the result of calling a block:array = [ 0, 5, 4, 7,转载 2014-06-23 20:48:27 · 357 阅读 · 0 评论 -
Boolean to check if array contains a pair that sums to 0
Just out of curiosity:def two_sum? a a.count(0) > 1 || (a & a.map(&:-@)).size > 1endtwo_sum? [1,2,3,4,-2] # ⇒ truetwo_sum? [1,0,3,4,0] # ⇒ truetwo_sum? [1,2,3,4,0] # ⇒ falsetwo_sum? [1,2,转载 2014-06-23 18:49:08 · 389 阅读 · 0 评论 -
Dividing fixnum to few parts in ruby
Since your example includes 000 which can only be a string (000 asFixnum is rendered as 0) you can easily split your numbers in group of 3 digits by doing:num = 10000 # your numres = num.to_s.rev转载 2014-06-25 08:44:58 · 354 阅读 · 0 评论 -
prime number test in Ruby
I have an array a = [1,2,3,4,5]. I want to test which of the numbers areprime and wanted to produce the output {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}.Any one liner will be appreciated.转载 2014-06-23 19:42:02 · 369 阅读 · 0 评论 -
Ruby array of two dimensional arrays, search/lookup?
This may be very simple, but I don't know all of Ruby's array functions.If I have a given array like:values = [["a", 1], ["b", 3], ["c", 7], ... etc ]I would like two functions:A function th转载 2014-06-25 07:56:00 · 527 阅读 · 0 评论 -
Hash#merge 把Key相同的键值装进数组
No need the custom_merge method. Ruby core supplied Hash#merge with block will help you out.h1 = {a: 1, c: 2} h2 = {a: 3, b: 5} h3 = h1.merge(h2){|k,o,n| [o,n]}h3# => {:a=>[1, 3], :c=>2, :b=>5转载 2014-06-25 08:22:00 · 418 阅读 · 0 评论 -
Generating a string with elements from an array based on pattern matching
I have an array like this:inList = ["edge_rabbit", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod", ...]How can I take the elements that end with _rabbit or elements with the fo转载 2014-06-23 18:34:29 · 369 阅读 · 0 评论 -
Slice array when element reached
Lets say I have an array like so:['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']I don't know the length of the array or when an 'x' will occur. When I reach 'x' I want to push the following el转载 2014-06-26 11:13:25 · 369 阅读 · 0 评论 -
Grouping consecutive numbers in an array
I need to add consecutive numbers to a new array and, if it is not a consecutive number, add only that value to a new array:old_array = [1, 2, 3, 5, 7, 8, 9, 20, 21, 23, 29]I want to get this resu转载 2014-05-24 21:10:15 · 381 阅读 · 0 评论 -
How to sort an array in Ruby to a particular order?
a=["one", "two", "three"]b=["two", "one", "three"]转载 2014-05-24 09:50:46 · 427 阅读 · 0 评论 -
Get indices for sorted permutation of an array in Ruby?
Let's say I have an Array ary = [0.0, 1.0, 5.0, 1.0, -2.0, 3.5], and I want as output another array of the same size containingary's indices in sorted-by-value-order. In other words, the output shou转载 2014-06-05 19:36:19 · 356 阅读 · 0 评论 -
How effectively join ruby hashes recieved from json lists
arr1 = [{ "device"=>100, "phone"=>"12345" },{ "device"=>102, "phone"=>"12346" }]arr2 = [{ "device"=>100, "type"=>"mobile", "name"=>"nokia" },{ "device"=>102, "type"=>"VIOP", "name"=>"smth" }]-----转载 2014-06-05 20:20:26 · 353 阅读 · 0 评论 -
using inject to count elements in array
I'm trying to count the occurrences of elements in an array and save it in a hash. I'd like to use the inject function. I have this code:a = ["the","the","a", "it", "it", "it"]a.inject(Hash.new(0))转载 2014-06-25 15:29:48 · 415 阅读 · 0 评论 -
Sort array by values in a separate hash
I have a hash mapping array indices to sort values, e.g.{0=>"item_ss", 1=>"item_hsj", 2=>"item_skls"}I have a separate array with line_item_id and associated line_items (tab separated) as foll转载 2014-06-27 09:18:44 · 604 阅读 · 0 评论 -
ruby字符串的encoding,force_encoding,encode,encode!
ruby字符串的encoding,force_encoding,encode,encode! 19 May 2013 ruby1.9开始对字符串编码支持已经比较完善,我们可以直接通过使用String类的实例方法 encoding,force_encoding,encode,encode!进行相关的编码操作。encodingruby1.9中为每个字符串对象增加了encodin转载 2014-06-06 19:23:11 · 795 阅读 · 0 评论