ruby中!!_Ruby反向! 功能

ruby中!!

逆转! Ruby中的功能 (reverse! function in Ruby)

As the name suggests, reverse! function is used to reverse the elements of an array. Most of the times, we need to reverse an array but if we do it with the help of loops, the program will become quite lengthy and time-consuming. Ruby facilitates you with reverse! function which produces the reverse of the array. Unlike, reverse function in Ruby, reverse! produces changes in the original array itself. It also returns the reverse of the array which can be stored in the corresponding new array for the fulfillment of future operations.

顾名思义, 反向! 函数用于反转数组的元素。 在大多数情况下,我们需要反转数组,但是如果在循环的帮助下完成该程序,则该程序将变得相当冗长且耗时。 Ruby为您提供反向帮助! 函数产生与数组相反的结果。 与Ruby中的反向功能不同, 反向! 在原始数组本身中产生更改。 它还返回数组的倒序,可以将其存储在相应的新数组中,以实现将来的操作。

Syntax:

句法:

 Array_name.reverse!

Now, let us understand the implementation concept in a much broader way with the help of program codes.

现在,让我们借助程序代码以更广泛的方式理解实现概念。

Example 1:

范例1:

=begin
Ruby program to demonstrate implementation of reverse! function
=end
# Initializing some arrays of elements 
Arr1 = ["apple", "mango", "banana", "apricot", "plum", "cherry"] 
Arr2 = ["one","two", "three", "four", "five"] 
Arr3 = [10,20] 
Arr4 = [110, 210, 310, 410, 510] 
Arr5 = ["C++", "Java", "C#", "Visual Basic", "Perl", "Python"]
Arr6 = ["Hrithik", "Satyam", "Amisha", "Kajal", "Wamp"] 
# Invoking reverse! function 
NewArr1 = Arr1.reverse!
NewArr2 = Arr2.reverse!
NewArr3 = Arr3.reverse!
NewArr4 = Arr4.reverse! 
NewArr5 = Arr5.reverse!
NewArr6 = Arr6.reverse!
# Printing the the corresponding arrays 
puts "#{NewArr1}"
puts "#{NewArr2}"
puts "#{NewArr3}"
puts "#{NewArr4}"
puts "#{NewArr5}"
puts "#{NewArr6}"

Output

输出量

["cherry", "plum", "apricot", "banana", "mango", "apple"]
["five", "four", "three", "two", "one"]
[20, 10]
[510, 410, 310, 210, 110]
["Python", "Perl", "Visual Basic", "C#", "Java", "C++"]
["Wamp", "Kajal", "Amisha", "Satyam", "Hrithik"]

Code logic:

代码逻辑:

In the above code, we have initialized six arrays. We are reversing them with the help of reverse! statement and storing the returned array in a new corresponding array. Eventually, we are printing the corresponding new arrays.

在上面的代码中,我们已经初始化了六个数组。 我们正在反转的帮助下反转它们 语句并将返回的数组存储在新的对应数组中。 最终,我们将打印相应的新数组。

Now, let us see verify whether reverse! function produces changes in the original array or not.

现在,让我们看看是否可以逆转! 函数是否在原始数组中产生更改。

Example 2:

范例2:

=begin
Ruby program to demonstrate implementation of reverse! function
=end
# Initializing some arrays of elements 
Arr1 = ["apple", "mango", "banana", "apricot", "plum", "cherry"] 
Arr2 = ["one","two", "three", "four", "five"] 
Arr3 = [10,20] 
Arr4 = [110, 210, 310, 410, 510] 
Arr5 = ["C++", "Java", "C#", "Visual Basic", "Perl", "Python"]
Arr6 = ["Hrithik", "Satyam", "Amisha", "Kajal", "Wamp"] 
# Invoking reverse! function 
NewArr1 = Arr1.reverse!
NewArr2 = Arr2.reverse!
NewArr3 = Arr3.reverse!
NewArr4 = Arr4.reverse! 
NewArr5 = Arr5.reverse!
NewArr6 = Arr6.reverse!
# Printing the the corresponding arrays 
puts "#{Arr1}"
puts "#{Arr2}"
puts "#{Arr3}"
puts "#{Arr4}"
puts "#{Arr5}"
puts "#{Arr6}"

Output

输出量

["cherry", "plum", "apricot", "banana", "mango", "apple"]
["five", "four", "three", "two", "one"]
[20, 10]
[510, 410, 310, 210, 110]
["Python", "Perl", "Visual Basic", "C#", "Java", "C++"]
["Wamp", "Kajal", "Amisha", "Satyam", "Hrithik"]

Code Logic:

代码逻辑:

In the above code, we have tried to show that reverse! function produces changes in the original array as well with the help of ‘puts’ statement through which we are printing the arrays.

在上面的代码中,我们试图证明这种相反! 函数会在原始数组中产生更改,并借助“ puts”语句来打印数组。

翻译自: https://www.includehelp.com/ruby/reverse-1-function.aspx

ruby中!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543771.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mcal rtm_RTM的完整形式是什么?

mcal rtmRTM:阅读手册 (RTM: Read The Manual) RTM is an abbreviation of "Read The Manual". RTM是“阅读本手册”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook,…

mysql悲观锁总结和实践

使用场景举例:以MySQL InnoDB为例商品t_goods表中有一个字段status,status为1代表商品未被下单,status为2代表商品已经被下单,那么我们对某个商品下单时必须确保该商品status为1。假设商品的id为1。 一、如果不采用锁,…

2G的完整形式是什么?

2G:第二代 (2G: Second Generation) 2G is an abbreviation of the "Second-Generation Cellular Network". 2G是“第二代蜂窝网络”的缩写 。 In 1991, 2G cellular networks were commercially introduced on the GSM standard in Finland by Radiolin…

推送证书

2019独角兽企业重金招聘Python工程师标准>>> 推送证书 1 openssl pkcs12 -in CertificateName.p12 -out CertificateName.pem -nodes 转换文件上传 2证书有效期 openssl x509 -in xxx.pem -noout -dates —反馈 notBeforeDec 12 07:42:27 2015 GMT notAfterDec 11…

ruby 覆盖率测试_Ruby方法覆盖

ruby 覆盖率测试Ruby中的方法重写 (Method overriding in Ruby) Method overriding simply means that there are two methods defined within the same scope and they both are used for performing different tasks. This feature is provided in an Object-oriented langua…

iOS 之 UITextField

UITextField 之 失去焦点 收起键盘 UITextField 之 手势收起键盘转载于:https://www.cnblogs.com/SimonGao/p/5106681.html

ruby推送示例_Ruby直到示例循环

ruby推送示例直到循环 (The until loop) The until loop is one of the great features of Ruby which makes it different from other programming languages. The support of until loop specifies how much user-friendly language Ruby is? 直到循环是Ruby的重要功能之一&…

Dubbo学习总结(4)——Dubbo基于Zookeeper实现分布式实例

入门实例解析 第一&#xff1a;provider-提供服务和相应的接口 创建DemoService接口 [java] view plaincopyprint? <span style"font-size:18px;">package com.unj.dubbotest.provider; import java.util.List; /** * 定义服务接口&#xff0c;该…

什么是5g全双工模式_5G的完整形式是什么?

什么是5g全双工模式5G&#xff1a;第五代 (5G: Fifth Generation) 5G is an abbreviation of the "Fifth Generation". 5G是“第五代”的缩写 。 It is the fifth-generation wireless technology for digital cellular networks that started broad operation in 2…

Gmap.net 怎么导入离线地图

我使用【http://www.cnblogs.com/enjoyeclipse/archive/2013/01/29/2882254.html】所提供的方式导出地图数据 但是在【C:\Users\用户名<你的计算机用户名>\AppData\Local\GMap.NET\TileDBv5\en】这个文件夹下看到的Data.gmdb永远是256m 用这个Data.gmdb文件也无法导入 请…

下一个全排列_下一个排列

下一个全排列Problem statement: 问题陈述&#xff1a; Given a permutation print permutation just greater than this. 给定一个排列&#xff0c;打印排列就比这个更大。 Example: 例&#xff1a; Permutation:1 3 2 5 4Output:1 3 4 2 5Solution: 解&#xff1a; What …

[转载]PhotoShop性能优化

现在随着Photoshop版本越来越高功能也越来越强大&#xff0c;而往往强大的功能需要电脑有好的配置运行&#xff0c;比如HDR、图像合成或者3D和视频等类似的功能&#xff0c;还有处理比较大尺寸的图像时&#xff0c;如果电脑配置不够强往往非常卡&#xff0c;这时我们就要好好设…

in-nan(ind)_NaN16 Constant in Julia

in-nan(ind)Julia| NaN16常数 (Julia | NaN16 Constant) NaN16 is a constant of the Float16 type in Julia programming language, it represents "not-a-number" value. NaN16是Julia编程语言中Float16类型的常量&#xff0c;它表示“非数字”值。 Syntax: 句法&…

iOS 架构模式

MVVM指南&#xff08;课程学习&#xff09; iOS 架构模式 iOS 之 依赖注入 Square对iOS App架构的新尝试---Ziggurat 基于彻底解耦合的实验性iOS架构转载于:https://www.cnblogs.com/SimonGao/p/5112299.html

分披萨问题_比萨疯狂问题

分披萨问题Problem statement: 问题陈述&#xff1a; There is a shop which sells pizza of three different sizes- Small, Medium, and Large Pizza. IncludeHelp is crazy for Pizzas. A small Pizza has an area of S unit2, a medium Pizza has an area of M unit2 and …

oracle 10g学习之分组函数

一、 &#xff08;1&#xff09;分组查询语句的顺序 select ... from ... where ... group by ... having ... order by ... 注意&#xff1a; where-->group by分组-->执行组函数-->having筛选->order by 如果select/having语句后面出现了组函数 那么se…

0.1uf与47uf并联_UF是什么形式?

0.1uf与47uf并联UF&#xff1a;超滤 (UF: Ultrafiltration) UF is an abbreviation of Ultrafiltration. It is a kind of membrane filtration which is used in UF water purifiers. Through a hollow fiber threaded semi-permeable membrane, the water is made to proceed…

机器学习相关——协同过滤

在现今的推荐技术和算法中&#xff0c;最被大家广泛认可和采用的就是基于协同过滤的推荐方法。本文将带你深入了解协同过滤的秘密。下面直接进入正题 1 什么是协同过滤 协同过滤是利用集体智慧的一个典型方法。要理解什么是协同过滤 (Collaborative Filtering, 简称 CF)&#x…

二维的完整形式是什么?

2D&#xff1a;二维 (2D: Two Dimensional) 2D is an abbreviation of "Two-Dimensional". 2D是“二维”的缩写 。 It is the dimension of any virtual object that has no manifestation of profundity. For example, if a graphic or picture representation of …

InfoQ中文站2015年度优秀社区编辑评选揭晓

\又到了年终岁末&#xff0c;在过去的一年里&#xff0c;InfoQ网站的月独立UV接近130万&#xff0c;月PV突破200万&#xff0c;每周独立访问用户接近30万&#xff0c;网站访问量过万的文章超过60篇。每月活跃的数十位社区编辑为InfoQ的内容生产贡献着力量。正是这点点汇聚的星光…