User-friendly Ruby count value

Source: Internet
Author: User

The count-taking method in Ruby is the most humane and flexible in the language I touch, which can be illustrated here for example:

First, print 7 times "Hello,world", as follows:

7.times{puts "Hello,world"} #在这里, puts function is equivalent to print+ "\ n"

Second, print 1 in turn. 9, there are more methods in Ruby, which are implemented in a simpler way, as follows:

(1..9). each{|i| Print I, ""}

Third, print 1. The value of 9 is smaller than 7, as follows:

1.upto (9) {|i| print I, "" If I < 7}

Cycle from 10 to 2 values, and then subtract 1 processing as follows:

10.downto (2) Do |i|print I, "" End

Five, 1-9 each number in turn, and then input the results, as follows:

A = 1..9b = a.collect{|i| i*2}p b[0..8]

Six, cycle from 3 to 19, and then add 3 processing, as follows:

3.step (19,3) do |i|print I, "" End

This article is from the "Fuqin Wine" blog, please make sure to keep this source http://yuhongchun.blog.51cto.com/1604432/1592632

User-friendly Ruby count value

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.