自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(191)
  • 资源 (1)
  • 问答 (1)
  • 收藏
  • 关注

转载 building an array from several ranges

[*70..89, *184..193, *224..233, *296..304, *336..345]Result:[70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 224

2015-05-24 09:47:41 508

转载 Ruby Array and .max

I'm very confused here.Why does ["4", "5", "29", "54", "4", "0", "-214", "542", "-64", "1", "-3", "6", "-6"].maxreturn 6 and not 542After deleting 6 from the array, then it returns 542

2015-05-24 09:45:13 492

转载 Slicing discontinuous data into continuous parts in Ruby

I have this discontinuous array:a = [1, 2, 3, 7, 8, 10, 11, 12]I need it to be an array of continuous arrays:[[1,2,3],[7,8],[10,11,12]]method 1:a = [1, 2, 3, 7, 8, 10

2015-05-24 09:38:44 387

转载 奇偶归一猜想

奇偶归一猜想(英语:Collatz conjecture),是指对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1。如n = 6,根据上述数式,得出序列6, 3, 10, 5, 16, 8, 4, 2, 1。(步骤中最高的数是16,共有8个步骤)如n = 8,根据上述数式,得出序列8, 4, 2, 1。(步骤中最高的数是8,共有3个步骤

2015-05-17 18:31:59 5832

转载 Finding sum of every combination of two numbers in an array in Ruby

I'm trying to create a function that checks if two numbers within an array have a sum of zero.[1,2,3,4,5] => false[1,2,3,-2,5] => truearr.combination(2).any? {|a, b| (a + b).zero? }

2015-05-17 16:56:29 421

转载 字符串输出某列

如有这么一个字符串rs =" Interface PHY Protocol InUti OutUti inErrors outErrors 10GE1/0/1 up

2015-05-16 10:29:48 329

转载 Sort an Array Mixed With Integers and Strings - Ruby

I have an array that must be sorted with low number to high number and then alphabetical order. Must use Array#sort_by i_want_dogs = ["I", "want", 5, "dogs", "but", "only", "have", 3]I want it t

2015-04-30 15:09:22 290

转载 Sorting array elements by date

dates = ["6/23/2014", "8/5/2014", "8/19/2014", "6/26/2014", "8/19/2014", "8/19/2014", "7/8/2014", "6/3/2014", "7/30/2014", "7/3/2014", "6/3/2014", "6/26/2014"]dates.sort_by { |

2015-04-30 15:00:29 218

转载 Sorting an array by category names

1、a = [ {id: 3, category_name: "Horror"}, {id: 4, category_name: "Non-Fiction"}, {id: 5, category_name: "LGBT"}, {id: 6, category_name: "Romance"}, {id: 7, category_name: "Romance"

2015-04-30 09:29:36 297

转载 How to format strings in an array in ruby?

array = [[0] "Bonk Radek S Male Green 6/3/1978",[1] "Bouillon Francis G Male Blue 6/3/1975",[2] "Smith Steve D Male Red 3/3/1985"]at the moment each string is formatted:Last Name, F

2015-04-30 09:27:28 309

转载 How do I sort an integer array while also keeping identical elements apart from each other?

Given this array:[38, 38, 40, 40, 40, 41, 41, 41, 41, 60]How do I sort it into this?[38, 40, 41, 60, 38, 40, 41, 40, 41, 41]-------------------------------------------------------

2015-04-30 09:19:00 285

转载 Sort an array alphabetically in Ruby but with capitalised words at the end

1、["Apple","banana","Zebra","orange"].sort_by(&:swapcase)Probably not a concern, but ["iPhone", "item"].sort_by(&:swapcase) #=> ["item", "iPhone"],["IRB", "It"].sort_by(&:swapcase) #=>

2015-04-30 09:07:02 346

转载 数字字母混合排序

a = ['1', '10', '100', '2', '42', 'hello', 'x1', 'x20', 'x100', '42x', '42y', '10.1.2', '10.10.2', '10.8.2']a.map {|i| i.gsub(/\d+/) {|s| "%08d" % s.to_i } }.zip(a).sort.map{|x,y| y}a.sort_by{|i|i.

2015-04-30 07:41:33 630

转载 using an enumerable built-in Ruby to access and manipulate nested data

array = [5, 10, [15, 20], 25, [30, 35, 40]array#method { #block that adds 5} => [10, 15, [20, 25], 30, [35, 40, 45]You could use a recursive lambda:add_five = lambda { |e| e.is_a?(Enumerable) ? e.m

2015-04-27 10:29:26 379

转载 Multiply all even indexed integers by two

You can also multiply alternate numbers by 2a = 44080412345679011.arr = a.to_s.chars.map.with_index {|n,i| i.even? ? n.to_i * 2 : n.to_i }# => [8, 4, 0, 8, 0, 4, 2, 2, 6, 4, 10, 6, 14, 9, 0,

2015-04-27 09:24:29 283

转载 Ruby: how to sort array of string parsing the content

Here is my problem: I have an array of string which contains data like that:array = ["{109}{08} OK", "{98} Thx", "{108}{0.8}{908} aa", "{8}{51} lorem ipsum"]I would li

2015-04-25 18:30:49 367

转载 Extracting Data from array of hashes Ruby

Given that I have the following array of hashes@response = { "0"=>{"forename_1"=>"John", "surname_1"=>"Smith"}, "1"=>{"forename_1"=>"Chris", "surname_1"=>"Jenkins"},

2015-04-18 23:20:42 362

转载 Get max consecutive occurrences of value in array

Is there a more elegant way to achieve this below:Input:array = [1, 1, 1, 0, 0, 1, 1, 1, 1, 0]Output:4---------------------------------------------------Similar to w0lf's ans

2015-04-18 18:31:11 337

转载 How to combine the value of multiple hashes within an array by the same key

I have an array of hashes like so:[{"apple"=>5}, {"banana"=>4}, {"orange"=>6}, {"apple"=>4}, {"orange"=>2}]How I do get to:[{"apple"=>9},{"banana"=>4},{"orange"=>8}]-----------------

2015-04-16 10:21:57 368

转载 Getting the indexes of duplicate elements in arrays (Ruby)

I have an array in Ruby that has some duplicate elements. E.g.:fruits = ["apples", "bananas", "apples", "grapes", "apples"]-------------------------------------------------------------------fruits

2015-04-15 15:46:19 327

转载 Abbrev

http://ruby-doc.org/stdlib-2.1.2/libdoc/abbrev/rdoc/Abbrev.htmlAbbrevCalculates the set of unique abbreviations for a given set of strings.require 'abbrev'require 'pp'pp Abbrev.abbrev([

2015-03-13 15:53:27 607

转载 Custom usage of %w in Ruby

I know that %w or %W is used for constructing an array of strings. But I have a custom requirement:Using %w, I would like to get the following array pattern:["First Name", "Middle Name", "La

2015-03-03 15:53:58 394

转载 替换多个单词

Given:check_for = ["Lorem", "ipsum", "dolor", "sit", "amet"]replace_with = ["Donec", "ut", "libero", "sed", "arcu"]sentence = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a di

2015-03-03 09:19:16 392

转载 get an array of arrays with unique elements

get an array of arrays with unique elementsI have an array like this:[1, 2, 3, 3, 4, 4, 5, 6, 6, 6, 7]I want to know if there's a method to get this:[[1, 2, 3, 4, 5, 6, 7], [3, 4, 6]

2015-02-24 18:11:20 376

转载 js-常用替换“单引号”、“双引号”、“尖括号

js-常用替换“单引号”、“双引号”、“尖括号”…… var str="str=str.replace(/\'/g,"’");//替换半角单引号为全角单引号str=str.replace(/\"/g,"”");//替换半角双引号为全角双引号str=str.replace(//g,"《").replace(/>/g,"》");//

2015-01-09 15:17:21 18705 1

转载 JS未结束的字符串常量

未结束的字符串常量1.JAVASCRIPT引用时,使用的字符语言不一致. 比如:.xxx.js文件内部使用的是GB2312的格式,外面调用使用的是UTF-8,所以文件内部部分特殊字符因为格式不一致,出现乱码,造成此原因.2.JAVASCRIPT输出HTML字符时, 前后标记不匹配. 这种比较常见,往往在输出字符串时,出现单引号(’)或双引号(”)不配对,或者是在docu

2015-01-09 15:15:42 439

转载 Number of days between two Date objects (Ruby)

How can I find the number of days between two Date objects?irb(main):005:0> a = Date.parse("12/1/2010")=> #irb(main):007:0> b = Date.parse("12/21/2010")=> #irb(main):016:0> c = b.mjd - a.mjd

2014-12-01 19:55:44 412

转载 Ruby更改gem source

Ruby更改gem source博客分类: ruby  很多时候,在安装gem的过程中会出现找不到资源的error,我们需要从另外一个gem服务器下载安装。通过gem sources命令配置源,或通过修改Gemfile中的source语句可以实现。常用的源http://rubygems.org/http://gems.github

2014-11-02 19:42:49 362

转载 OpenTextFile

Visual Basic for Applications ReferenceVisual Studio 6.072 out of 190 rated this helpful - Rate this topicOpenTextFile MethodSee Also    Example    Applies To    Specific

2014-10-30 20:17:26 617

原创 IP地址与整数的相互转换

require 'ipaddr'

2014-10-20 16:52:27 714

转载 in Ruby, conversion of float integer into %H %M %S time

How do you convert a float, say 13.5, to the corresponding 24-hour time %H:%M:%S? (13.5 would be 13:30:00, 8.25 would be 8:15:00) I'm still figuring the Time class...it confuses mesec = (13.5 *

2014-10-16 15:02:20 414

转载 how to remove nil and blank string in an array in Ruby

I am new to Ruby and stuck with this issue. Let's say I have an array like this:arr = [1, 2, 's', nil, '', 'd']and I want to remove nil and blank string from it, i.e. final array should be:arr

2014-10-14 10:25:26 413

转载 Seeing numbers in Ruby with r attached to the number

0.1r #=> (1/10)0.1r * 3 #=> (3/10)

2014-10-13 10:30:36 335

转载 the different of Hash.new([]) and Hash.new{[]}

Consider this code:h=Hash.new(0) #new hash pairs will by default have 0 as valuesh[1]+=1 # {1=>1}h[2]+=2 # {2=>2}that's all fine, but:h=Hash.new([]) #empty array as default valueh[1]1 #{1=>

2014-10-09 13:18:33 316

转载 How to get the HTML source of a webpage in Ruby

Use Net::HTTP:require 'net/http'source = Net::HTTP.get('stackoverflow.com', '/index.html')

2014-10-07 09:05:13 540

转载 Ruby: How to find all indices of elements that match a given condition?

Ruby 1.9:arr = ['x', 'o', 'x', '.', '.', 'o', 'x']p arr.each_index.select{|i| arr[i] == 'x'} # =>[0, 2, 6]Code

2014-10-06 23:03:51 415

转载 How to sum values in an array with different hash

I want to sum the total values of the same items in an array.I have a array as[{"a"=>1},{"b"=>2},{"c"=>3},{"a"=>2},{"b"=>4}]I want to get the result as[{"a"=>3},{"b"=>6},{"c"=>3}]Which

2014-10-02 14:49:56 286

转载 Check if array element appears on line

I'm going through a file line by line and I want to check if that line contains any element from an array. for instance if I have:myArray = ["cat", "dog", "fish"]and the current line said:

2014-09-28 20:22:32 316

转载 Obtain Key from a Hash of Arrays using array value

I have a Hash as follows:{'a' => [title: 'ab', path: 'cd'], 'b' => [title: 'ef', path: 'gh']}Now lets say I have one title and wish to get the key from it...i.e. if I have 'ef' and want

2014-09-27 11:38:07 351

转载 Ruby converting array of hashes to array of arrays

I have a ruby array of hashesarr1 = [:a => {:name=>"Bob",:age=>"10",:city=>"NY"}, :b => {:name=>"Mike",:age=>"20",:city=>"FL"}]What is the best way to convert this toarr2 = [["Bob",10]

2014-09-26 09:00:13 319

AdbeRdr930_zh_CN

ADOBE公司出品的PDF文件阅读器,非常好用!

2012-04-20

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除