原创 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 instanc

原创 Seeing numbers in Ruby with r attached to the number

0.1r #=> (1/10) 0.1r * 3 #=> (3/10) (0.5r..2r).step(0.1r).map &:to_f #=> [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2,

原创 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

原创 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') require 'open-uri' sourc

原创 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 wou

原创 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

原创 IP地址與整數的相互轉換

require 'ipaddr' ip_int=IPAddr.new("192.168.1.1").to_i int_ip=IPAddr.new(iptoint, Socket::AF_INET).to_s 應用: require 'i

原创 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") => #<Date: 49

原创 Ruby: How to find all indices of elements that match a given condition?

Given an array, how would you find all indices of elements those match a given condition? For example, say we have:

原创 JS未結束的字符串常量

未結束的字符串常量 1.JAVASCRIPT引用時,使用的字符語言不一致.  比如:<script. type=”text/javascript” src=”xxx.js” charset=”UTF-8″>.xxx.js文件內部使用

原创 the different of Hash.new([]) and Hash.new{[]}

Consider this code: h=Hash.new(0) #new hash pairs will by default have 0 as values h[1]+=1 # {1=>1} h[2]+=2 # {2=>2}

原创 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 b

原创 Ruby converting array of hashes to array of arrays

I have a ruby array of hashes arr1 = [:a => {:name=>"Bob",:age=>"10",:city=>"NY"}, :b => {:name=>"Mike",:age=>

原创 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},

原创 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