ruby array

Array.select! 方法 (Array.select! Method)

In this article, we will study about Array.select! Method. You all must be thinking the method must be doing something related to the selection of objects from the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

在本文中,我们将研究Array.select! 方法 。 你们都必须认为该方法必须在做一些与从Array实例中选择对象有关的事情。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby's library. This method, as the name suggests, is used to select some elements from the Array. This method is destructive and brings changes in the actual values of the Array object. This method works based on a certain condition which you will provide inside the pair of parenthesis. This method is based on the criteria you provide inside the block. This method will not work if you do not specify any condition inside the block. Though it will not throw an exception you will get nil as the result. So, if you are working with this method you should be having a certain condition based on which the element is going to be selected from the object of Array class. If you want to print all the elements of Array instance then you can go for Array.each method and avoid going for this one.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 顾名思义,此方法用于从数组中选择一些元素。 此方法具有破坏性,并会更改Array对象的实际值。 此方法基于您将在圆括号内提供的特定条件。 此方法基于您在块内提供的条件。 如果未在块内指定任何条件,则此方法将不起作用。 尽管它不会引发异常,但您将得到nil作为结果。 因此,如果您正在使用此方法,则应该具有一定的条件,根据该条件要从Array类的对象中选择元素。 如果要打印Array实例的所有元素,则可以使用Array.each方法,并避免使用该方法。

Syntax:

句法:

    array.select!{|var| #condition}

Argument(s) required:

所需参数:

This method does not permit the passing of any arguments instead it mandates a condition.

此方法不允许传递任何参数,而是要求一个条件。

Example 1:

范例1:

=begin
Ruby program to demonstrate Array.select! method
=end
# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]
puts "Enter 'a' for Even numbers and 'b' for odd numbers"
opt = gets.chomp
if opt == 'a'
puts "Even numbers are:"
puts num.select!{|num|
num%2 == 0
}
puts "Array instance: #{num}"
elsif opt == 'b'
puts "Odd numbers are:"
puts num.select!{|num|
num%2 !=0
}
puts "Array instance: #{num}"
else
puts "Wrong selection. Input valid option"
end

Output

输出量

RUN 1:
Enter 'a' for Even numbers and 'b' for odd numbers
a
Even numbers are:
2
44
2
12
90
78
Array instance: [2, 44, 2, 12, 90, 78]
RUN 2:
nter 'a' for Even numbers and 'b' for odd numbers
b
Odd numbers are:
5
7
83
5
67
11
9
Array instance: [5, 7, 83, 5, 67, 11, 9]

Explanation:

说明:

In the above code, you can observe that we are taking input from the user about what type of numbers the user wants as the output. This is because we want to pass certain conditions inside the Array.select! method. We are giving responses to the user as per the option provided by the user and this method is used in this way only. You can see that this method is creating a direct impact on the self Array instance because this method is one of the examples if destructive methods.

在上面的代码中,您可以观察到我们正在从用户那里获取用户想要输入什么类型的数字作为输入。 这是因为我们要在Array.select内部传递某些条件 方法 。 我们根据用户提供的选项向用户提供响应,并且仅以这种方式使用此方法。 您可以看到此方法对self Array实例产生直接影响,因为该方法是破坏性方法的示例之一。

Example 2:

范例2:

=begin
Ruby program to demonstrate Array.select!
=end
# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]
puts num.select!{|a|}

Output

输出量

No Output.

Explanation:

说明:

In the above output, you can observe that when you are not specifying any condition inside the method, then you are not getting anything as the output.

在上面的输出中,您可以观察到,当您未在方法内部指定任何条件时,则不会得到任何输出。

翻译自: https://www.includehelp.com/ruby/array-select-inverse-method-with-example.aspx

ruby array

ruby array_Array.select! Ruby中的示例方法相关推荐

  1. rotate array_Array.rotate! Ruby中的示例方法

    rotate array Array.rotate! 方法 (Array.rotate! Method) In this article, we will study about Array.rota ...

  2. zemax微透镜阵列示例_阵列反向! Ruby中的示例方法

    zemax微透镜阵列示例 阵列反向! 方法 (Array reverse! Method) In this article, we will study about Array.reverse! me ...

  3. Javascript 操作select标记中options的方法

    1 检测是否有选中 if(objSelect.selectedIndex > -1) { //说明选中 } else { //说明没有选中 } 2.动态创建select function cre ...

  4. ruby hash方法_Ruby中带有示例的Hash.select方法

    ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...

  5. ruby array_Ruby中带有示例的Array.select方法

    ruby array Array.select方法 (Array.select Method) In the last articles, we have seen how to iterate ov ...

  6. ruby hash方法_Ruby中带有示例的Hash.invert方法

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

  7. ruby array_Ruby中带有示例的Array.cycle()方法

    ruby array Array.cycle()方法 (Array.cycle() Method) In this article, we will study about Array.cycle() ...

  8. ruby中、.reject_Ruby中带有示例的Array.reject方法

    ruby中..reject Ruby Array.reject方法 (Ruby Array.reject Method) In the last article, we have seen how w ...

  9. ruby hash方法_Ruby中带有示例的Hash.flatten方法

    ruby hash方法 哈希平化方法 (Hash.flatten Method) In this article, we will study about Hash.flatten Method. T ...

最新文章

  1. COM:养分平衡在塑造植物根-真菌互作中的作用:事实与猜想
  2. 【c语言】输入输出格式练习
  3. oracle中同义词的用法,Oracle中使用同义词介绍
  4. Basic Level 1018. 锤子剪刀布 (20)
  5. 广州站 | 云原生 Serverless 技术实践营精彩回顾
  6. python sklearn 归一化_第3章 Sklearn概述
  7. php 获取汉字,php 获取汉字长度
  8. 《Java 核心技术卷1 第10版》学习笔记------调试技巧
  9. 阿里双11大促秒杀活动下的缓存技术与高水位限流实现
  10. pycharm+anaconda编译器运行程序时一直显示“ImportError: DLL load failed: 找不到指定的模块”的解决办法
  11. ActiveMQ消息队列介绍(转)
  12. python中的fun_Python fun中*args,**kwargs参数的含义和用法(*args,**kwargs),Pythonfunargskwargs,及...
  13. CDMA关键技术(RAKE、软切换、功率控制
  14. Racket编程指南——2 Racket概要
  15. 十月美剧精听总结 - 权力的游戏「Game of Throne」 黑袍纠察队「The boys」 老无所依「No Country for the old men」
  16. Python3:批量读取excel百度分享链接保存到百度网盘
  17. ANSI颜色使用研究 (转)
  18. 阿里天池大数据竞赛第一名,如何用AI检测肺癌
  19. Ubuntu 16.04 编译Android 6.0系统源码
  20. 使用IDM下载磁力链或迅雷文件

热门文章

  1. python读写文件操作_详解Python文件读写操作
  2. 【Java从入门到头秃专栏 6】语法篇(五) :多线程 线程池 可见、原子性 并发包 Lambda表达式
  3. 读卡距离和信号强度两方面来考虑
  4. [转] Lodash
  5. C# WinForm开发系列 - GDI+【转】
  6. Python基础--环境配置、编码风格、基础概念、基本数据类型(1)
  7. intellij idea 最常用的快捷键
  8. ISP运营商实验室测试机架拓扑搭建经验分享
  9. 浏览器兼容问题 透明度 position:fixed bootstrap
  10. Binary XML file line #2: You must supply a layout_height attribute inflate