Category:Ruby
-
A preliminary introduction to Ruby multithreaded programming
Traditional programs have a separate thread to execute, and the statements or instructions containing the program are executed sequentially until the program terminates. A multithreaded program has the execution of multiple threads. Each thread is executed sequentially, but on a multi-core CPU machine, threads may execute in parallel. For example, generally, in a single CPU […]
-
Common file operation methods in Ruby
1、 New file Copy codeThe code is as follows: f=File.new(File.join(“C:”,”Test.txt”), “w+”) f.puts(“I am Jack”) f.puts(“Hello World”) File mode “r” :Read-only. Starts at beginning of file (default mode). “r+” :Read-write. Starts at beginning of file. “w” :Write-only. Truncates existing file to zero length or creates a new file for writing. “w+” :Read-write. Truncates […]
-
Using soap4r of ruby to write a tutorial of soap server
What isSOAP ? Simple object access protocol (soap) is a cross platform, language independent, XML based RPC Protocol, usually (but not necessarily) http. It uses XML to encode information, enabling remote procedure calls, HTTP over the network from clients toThe serverTo transmit information, and vice versa. Soap has several advantages over other technologies, such as […]
-
Collected multiple Ruby traversal folder code instances
1、 Traverse all files under the folder and output the file name Copy codeThe code is as follows: def traverse_dir(file_path) if File.directory? file_path Dir.foreach(file_path) do |file| if file !=”.” and file !=”..” traverse_dir(file_path+”/”+file) end end else puts “File:#{File.basename(file_path)}, Size:#{File.size(file_path)}” end end traverse_dir(‘D:/apache-tomcat’) 2、 Ruby […]
-
Ruby traverses the folder and calculates the md5sum of the file
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #!/usr/bin/ruby -w # require ‘digest/md5’ if ARGV.empty? puts “usgae: #$0 path” exit 0 end dir_name=ARGV.shift […]
-
Ruby common file operation code examples
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 […]
-
Quick sorting in one line of ruby code
Copy codeThe code is as follows: def quick_sort(a) return a if a.size < 2 (x = a.pop) ? quick_sort(a.select{|i| i <=x }) + [x] + quick_sort(a.select{|i| i > x}) : [] end array = [72,6,57,88,60,42,83,73,42,48,85] p quick_sort(array) #=> [6, 42, 42, 48, 57, 60, 72, 73, 83, 85, 88]
-
Three quick sorting algorithms implemented by ruby
When I first learned ruby, it happened that the algorithm teacher encouraged me to write algorithms in unfamiliar languages. I’ll use ruby~~Ruby is really powerful. Many intuitive methods can be used….. Worship infinity…. During this period, I encountered the error of invalid multibyte char (us-ascii). The solution is to add a #encoding: UTF-8 at the […]
-
Optimal binary search tree algorithm implemented by ruby
The pseudo code on the introduction of the algorithm is rewritten, plus the constructor of the solution of the first problem in the after-school practice of the introduction. Copy codeThe code is as follows: #encoding: utf-8 =begin author: xu jin date: Nov 11, 2012 Optimal Binary Search Tree to find by using EditDistance algorithm refer […]
-
Calculation method of the shortest editing distance implemented by ruby
Dynamic programming algorithm is used to calculate the shortest editing distance. Copy codeThe code is as follows: #encoding: utf-8 #author: xu jin #date: Nov 12, 2012 #EditDistance #to find the minimum cost by using EditDistance algorithm #example output: # “Please input a string: “ # exponential # “Please input the other string: “ # […]
-
Merge sorting algorithm implemented by ruby
Algorithm class homework, using divide and conquer, merge and sort. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #encoding: utf-8 #author: xu jin, 4100213 #date: Oct […]
-
Matrix multiplication algorithm implemented in Ruby
Dynamic programming solves the matrix multiplication problem, randomly generates the matrix sequence, and outputs the results in the form of ((A1 (a2a3)) (a4a5)). code: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 […]