Category:Ruby

  • Ruby + nokogori grab the first 10 pages of the embarrassing encyclopedia and store them in the database example

    Time:2020-4-2

    Ruby, nokogori, crawl the latest 10 pages of the Encyclopedia of embarrassing events and compare them with pictures and save them into the text and database at the same time Copy codeThe code is as follows: #encoding:utf-8require “open-uri”require “nokogiri”require “mysql”@dbh=Mysql.real_connect(“localhost”,”root”,”dengli”,”pachong”)@file=open(“qb.txt”,”w”)def getQiubai(url)  data=open(url){|f| f.read}  doc=Nokogiri::HTML(data)  doc.css(‘div.block.untagged.mb15.bs2’).each{|p|    content=p.css(‘div.content’).text    @file.puts(“#{content}\n”)    img=p.css(‘div.thumb img’).each{|img|       imgAddr=img.attr(‘src’)        @file.puts(“#{imgAddr}\n”)       time=Time.now       sql=”INSERT INTO […]

  • An example of ruby judging whether a number is prime

    Time:2020-4-1

    Ruby determines whether a number is prime Prime numbers are also called prime numbers. A natural number greater than 1, if it can not be divided by other natural numbers except 1 and itself; (except 0), otherwise it is called a composite number. According to the basic theorem of arithmetic, every integer larger than 1 […]

  • Example of producer consumer problem implemented by ruby thread (thread synchronization implemented by queue)

    Time:2020-3-31

    Ruby thread implements the classic producer consumer problem, and uses the queue class in ruby to implement the thread synchronization problem. Copy codeThe code is as follows: require “thread”  puts “ProAndCon”  Queue = queue.new – use queue to synchronize threads producer = Thread.new do      10.times do |i|  Sleep range (I) allows a thread to […]

  • Example of stone scissors game implemented by ruby

    Time:2020-3-30

    Ruby realizes the game of stone scissors Copy codeThe code is as follows: #encoding: utf-8Arr = [‘stone’, ‘scissors’,’ cloth ‘]Win_arr = [‘stone’, ‘scissors’], [‘ scissors’, ‘cloth’], [‘cloth’, ‘stone’]]#The value of random computer, put into the result arrayresult = [arr.sample]while (true)“Please input stone, scissors and cloth”  input_value = gets.force_encoding(“GBK”).encode(“UTF-8”).chomp  if arr.include? input_value    result << input_value    if […]

  • Example of ruby using restclient to upload server local files

    Time:2020-3-29

    Using restclient to upload server local files Copy codeThe code is as follows: Url =’http: / / XX ‘ාpost to URLField_hash = {key: key, token: token} post dataRequest = restclient.post URL, field ﹐ hash.merge (file: file. New (file. Join (‘public ‘,’ {params [: logo]} ‘),’rb’)) ﹐ upload file Reponse = request.to ා return result

  • A general solution to the failure of ruby to install gem package

    Time:2020-3-28

    Ruby language upgrade is more diligent. However, a large number of versions make the compatibility of libraries a big problem. Some gems explicitly do not support a particular version of ruby, while others are not compatible with a higher version. Plus, gem has its own version. It’s a mess. Even if you use Ruby version […]

  • Notes on Ruby on rails (the whole process of installation and use)

    Time:2020-3-27

    There is a chance to try rails again, but the original contact is 2, now it has become 4, it seems that the installation will be faster than the original.. Rails 4 installation For RVM installed Copy codeThe code is as follows: gem install rails If not, it should be like this: Copy codeThe code […]

  • Summary of usage of percentage sign (%) in Ruby

    Time:2020-3-26

    %Q A string used to replace double quotes. When you need to put many quotes in a string, you can use the following methods directly without adding backslashes (\ “) one by one before the quotes Copy codeThe code is as follows: >> %Q(Joe said: “Frank said: “#{what_frank_said}””)=> “Joe said: “Frank said: “Hello!””” (…) can […]

  • Several open source projects of ruby wechat development

    Time:2020-3-25

    Recently, many people who use rails to develop wechat projects began to use Weixin rails middleware and Weixin authorize on GitHub, and many people talked about ratchet. Since the Spring Festival, wechat has been developed. Now it is published on the forum, hoping to let more people know the existence of these gems and complete […]

  • A simple Ruby reversible encryption and decryption class

    Time:2020-3-24

    Implementation code: Copy codeThe code is as follows: class Des    require ‘openssl’    require ‘base64’    ALG = ‘DES-EDE3-CBC’    KEY = “mZ4Wjs6L”    DES_KEY = “nZ4wJs6L”  Encryption  def encode(str)      des = OpenSSL::Cipher::Cipher.new(ALG)      des.pkcs5_keyivgen(KEY, DES_KEY)      des.encrypt      cipher = des.update(str)      cipher << des.final  Return base64.encode64 (cipher) ාාbase64 can be saved […]

  • Ruby based insertion and bubble sorting algorithms

    Time:2020-3-23

    1. Insert sort Copy codeThe code is as follows: seq = [3,4,9,0,2,5,9,7,1] 1.upto(seq.length-1) do |i|  if seq[i] < seq[i-1]    tmp = seq[i]    j = i-1    while(j>=0 && tmp<seq[j]) do      seq[j+1] = seq[j]      j=j-1    end    seq[j+1]=tmp  endend seq.each {|num| puts num} 2. Bubble sorting Copy codeThe code is as follows: seq = [3,4,9,0,2,5,9,7,1,100,99,24,64,18,88,66] 0.upto(seq.length-1) do |i|  […]

  • How to write a beautiful rake file

    Time:2020-3-22

    Rake, I won’t talk about Ruby’s make any more than make in many aspects. Unlike Makefile, rakefile itself is a piece of ruby code, which has many advantages. On the one hand, it can do anything Ruby can do directly in rake. On the other hand, because Ruby supports DSL well, rakefile usually does not […]