• Various Sorting Algorithms Implemented by Ruby


    Time complexity: _(n ^ 2) Bubble sort Copy codeThe code is as follows: def bubble_sort(a)    (a.size-2).downto(0) do |i|      (0..i).each do |j|        a[j], a[j+1] = a[j+1], a[j] if a[j] > a[j+1]      end    end    return a  end Selection sort Copy codeThe code is as follows: def selection_sort(a)    b = []  […]

  • Ruby implements code sharing between producers and consumers


    # Ruby implements producer and consumer code require ‘thread’ queue = consumers = do 5.times do |i| obj = queue.pop print “consumer :#{i}\n” sleep(rand(0.05)) end end producters = do 5.times do |i| sleep(0.1) print “producter : #{i}\n” queue.push(“Item #{i}”) end end producters.join consumers.join

  • Introduction to the Differences of Requirement, Load, Inclusion and Extension in Ruby


    Require, load is used for files at the end of a file, such as. rb, etc. Include, load is used to contain modules in a file.Requirement is generally used to load library files, while load is used to load configuration files. 1、require:Load a library and load it only once. If you load it many times, […]

  • Two Differences between Proc and Lambda in Ruby


    1. In proc and lambda, return keyword has different meanings:In proc, return simply means return from this lambda.In lambda, return is not returned from proc, but from the scope in which proc is defined. Copy codeThe code is as follows: def one_methodWhen P = Proc. new {return 10} # calls this block, it returns 10 […]

  • Comparisons between class variables and instance variables in Ruby


    1. Class variable names start with @, and a class variable is shared by all instance objects of this class; instance variable names start with @, and each instance object has its own instance variables. 2. Class variables must be initialized before they are used, otherwise they will report errors when they are used, while […]

  • Ruby defines two ways of private methods


    # Define Private Approach 1: class C def public_method private_method end def private_method end Private: private_method Defines the method as private end # Define Private Approach 2: class C def public_method private_method end private Def private_method Defines private methods end end

  • Brief introduction to basic data types of ruby


    The basic data types in generalization and ruby are all class types, but for the convenience of writing and reading, they provide a more concise way of writing for the basic data types. I. NUMERICAL TYPES Integers support binary, octal, decimal and hexadecimal systems, and dynamically determine whether an integer is Fixnum or Bignum based […]

  • Introduction of ruby’s program structure


    The summary and Ruby program consists of one or more Ruby source files. The source files are composed of modules. There are control structure, class object, method, expression and other elements in the module. The following is a summary of ruby‘s program organization structure according to this idea. I. Source Documents 1.1. File Inclusion   […]

  • Return, break, next in Ruby


    The use of return, break and next involves the problem of jumping out of scope, and their difference lies in the different purpose scope of jumping out of different keywords, because there are some places that need special attention because of code blocks. return Common Ways Usually the return statement means the same thing as […]

  • Detailed explanation of hook method in Ruby


    Ruby’s philosophy is based on the fundamental element of making programmers happy. Ruby attaches great importance to programmer’s happiness and offers many different ways to achieve it. Its metaprogramming ability enables programmers to write code generated dynamically at run time. Its threading function gives programmers an elegant way to write multithreaded code. Its hook method […]

  • Ruby’s 25 programming details (tips, practical code snippets)


    1. try never throws an exception and returns nil when it doesn’t Copy codeThe code is as follows: province_id = Province.find_by_name(prov).try(:id)  2. The find (: first,: condotions) method goes without saying. Copy codeThe code is as follows: mobile_info = MobileInfo.find(:first, :conditions => [“mobile_num = ? “, mobile_num.to_i])  3.find(:all, :select, :conditions) Copy codeThe code is as […]

  • Detailed Explanation of Lover Method_missing Method in Ruby Metaprogramming Dream


    I recently read articles (such as this one) promoting the use of method_missing in Ruby. Many people are burning with method_missing, but they are not careful about their relationship. So I would like to discuss this question: ** How do I use method_missing** When to resist the temptation of method_missing First, never give in to […]