Ruby special syntax concepts

Source: Internet
Author: User

The Ruby language has many special functions that deserve further research. For example, the syntax is different from that of other programming languages. Here we will understand some concepts related to Ruby's special syntax.

  • Basic Syntax of common Ruby Libraries
  • Using pp () to output two-dimensional arrays in Ruby
  • Ruby on Rails directory files
  • Ruby function pointer concepts
  • Ruby Rmagick Installation Guide

The question is looking for change. Suppose we need to find another person with a change of 39 USD,
The result will be (dollar coins include, 10, 5, and 1 ):

To solve this problem with the special Ruby syntax, you only need two lines,

 
 
  1. Def make_change (amount,
    Coins = [])
  2. Coins. sort! {| X, y | y <=> x}
  3. Return coins. map! {| Coin |
  4. F = amount/coin; amount
    % = Coin; Array. new (f) {coin}
    }. Flatten
  5. End
  6. P make_change (39, [10, 1, 5, 25])

The greedy algorithm is used here. The biggest coin is used to divide each time, and the remaining money is used for the same calculation with the next coin.

A special Ruby syntax is the most flexible and agile language I have ever seen.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.