ruby 执行函数

Ruby中的at()函数 (at() function in Ruby)

If you are working with arrays in Ruby, sometimes you may need to find the element at a particular index. For meeting the purpose, we have got at() function in Ruby which is already defined in Ruby's library. The basic purpose of at() function is to return the element stored at the specified index which is passed as an argument. The index may be positive, negative or zero. If we invoke at() function with 0 index, it will return the first element of the array, likewise, if we invoke at() function with 1 index, it will return the second element of the array. In case of negative indexes, at() with Index -1 will return the last element of the array, at() with Index -2 will return the second last element of the array, at() with Index -3 will return the third last element of the array and so on subject to availability.

如果您在Ruby中使用数组 ,则有时可能需要在特定索引处找到该元素。 为了达到目的,我们在Ruby的库中已经定义了Ruby中的at()函数at()函数的基本目的是返回存储在指定索引处的元素,该元素作为参数传递。 索引可以为正,负或零。 如果调用索引为0的at()函数 ,它将返回数组的第一个元素,同样,如果调用索引为1的at()函数 ,它将返回数组的第二个元素。 如果为负索引,则索引为-1的at()将返回数组的最后一个元素,索引为-2的at()将返回数组的第二个元素,索引为-3的at()将返回第三个元素。数组的最后一个元素,依情况而定。

Syntax:

句法:

 Array_name.at(Index)

Now, let us understand the implementation of at() function with the help of provided examples.

现在,让我们借助提供的示例了解at()函数的实现。

Example 1:

范例1:

=begin
Ruby program to demonstrate implementation of at() function
=end
# Initialising array of elements
Arr = ["C++", "C", "Perl", "Python", "Java", "Visual Basic","C#", "Ruby", "Includehelp"]
# Calling to at() function
Var1 = Arr.at(0)
Var2 = Arr.at(1)
Var3 = Arr.at(3)
Var4 = Arr.at(5)
Var5 = Arr.at(-1)
Var6 = Arr.at(-3)
# Getting the corresponding elements
# whose indexes are given as parameter
puts "#{Var1}"
puts "#{Var2}"
puts "#{Var3}"
puts "#{Var4}"
puts "#{Var5}"
puts "#{Var6}"

Output

输出量

C++
C
Python
Visual Basic
Includehelp
C#

Code logic:

代码逻辑:

In the above code, we have initialized an array named 'arr'. We are invoking at() with different indexes as an argument. arr.at(0) is returning the first element of the array and coming towards negative indexes, at(-1) is returning the last element of the array.

在上面的代码中,我们初始化了一个名为“ arr”的数组。 我们以不同的索引作为参数调用at()arr.at(0)返回数组的第一个元素并指向负索引, at(-1)返回数组的最后一个元素。

Example 2:

范例2:

=begin
Ruby program to demonstrate implementation of at() function
=end
# Initialising array of elements
Arr = ["C++", "C", "Perl", "Python", "Java", "Visual Basic","C#", "Ruby", "Includehelp"]
#Taking index from user
puts "Enter the index of the element you want to search"
i = gets.chomp.to_i
puts "The element at index #{i} is #{Arr.at(i)}"

Output

输出量

Run 1:
Enter the index of the element you want to search
3
The element at index 3 is Python
Run 2:
Enter the index of the element you want to search
-1
The element at index -1 is Includehelp

Code logic:

代码逻辑:

You can observe in the above code that we are taking an index as an input from the user and invoking at() function with that index. The function is returning values corresponding to that index.

您可以在上面的代码中观察到,我们将索引作为用户的输入,并使用该索引调用at()函数 。 该函数正在返回与该索引对应的值。

Example 3:

范例3:

=begin
Ruby program to demonstrate implementation of at() function
=end
# Initialising array of elements
Arr = ["C++", "C", "Perl", "Python", "Java", "Visual Basic","C#", "Ruby", "Includehelp"]
#Storing value in the variable
p=Arr.at(1)
q=Arr.at(3)
puts p
puts q

Output

输出量

C
Python

Code logic:

代码逻辑:

We are invoking at() function inside variables. The variable is storing the value which is returned from the at() method. The value can be used for further processing.

我们正在调用变量内部的at()函数 。 该变量存储从at()方法返回的值。 该值可用于进一步处理。

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

ruby 执行函数

ruby 执行函数_Ruby at()函数相关推荐

  1. R语言使用dplyr包使用group_by函数、summarise函数和mutate函数计算分组下的均值、标准差、样本个数、以及分组均值的95%执行区间对应的下限值和上限值(Calculate CI)

    R语言使用dplyr包使用group_by函数.summarise函数和mutate函数计算分组下的均值.标准差.样本个数.以及分组均值的95%执行区间对应的下限值和上限值(Calculate 95% ...

  2. JavaScript 函数声明,函数表达式,匿名函数,立即执行函数之区别

    函数声明:function fnName () {-};使用function关键字声明一个函数,再指定一个函数名,叫函数声明. 函数表达式 var fnName = function () {-};使 ...

  3. linux驱动入口函数执行了,probe函数没有执行排查

    写了个spi1的驱动函数,insmod后发现没有反应. 添加打印信息后,发现执行了驱动入口函数(在入口函数中执行了spi_register_driver),但是probe函数没有执行,检查compat ...

  4. oracle中执行动态sql语句吗,oracle中有没有可动态执行sql语句的函数

    oracle中有没有可动态执行sql语句的函数 关注:233  答案:2  手机版 解决时间 2021-03-05 15:53 提问者祗剩寂寞 2021-03-04 22:38 oracle中有没有可 ...

  5. python装饰器函数-python 装饰器 函数被装饰+函数执行

    """ func1.py 文件 1.执行到@w1 2.被装饰函数作为装饰器参数 w1(func=f1()) 3.w1 函数执行有返回值,得用变量接收,变量名为f1 合适 ...

  6. java main 方法不能执行,AndroidStudio无法执行Java的main函数

    AndroidStudio无法执行Java的main函数 AndroidStudio无法执行Java的main函数 FAILURE: Build failed with an exception. * ...

  7. 函数声明、函数表达式、匿名函数、立即执行函数

    函数声明:function fnName () {-};使用function关键字声明一个函数,再指定一个函数名,叫函数声明. 函数表达式 var fnName = function () {-};使 ...

  8. settimeout( )是全局函数吗_JS函数的执行

    js 在不同时机运行,会有不同的运行结果,本文将分别举例分析 案例1 只有在调用fn()时函数才开始执行 函数可以调用全局变量a 案例2 再次声明,在调用fn()是才开始执行函数,所以执行完 a=2后 ...

  9. Python 函数的执行流程-函数递归-匿名函数-生成器

    1 函数的执行流程 函数的执行需要对函数进行压栈的,什么是压栈呢,简而言之就是在函数执行时在栈中创建栈帧存放需要变量以及指针的意思.具体涉及的知识非常多,这里就已一个Python脚本简单进行分析. 当 ...

最新文章

  1. leetcode刷题之树(三)
  2. 动态规划(dynamic programming)基础【背包问题】
  3. (史上最全)104个实用网络爬虫合集。
  4. 关于redis内存分析,内存优化
  5. 最棒的Java开发企业级权限管理系统项目实战
  6. UDS学习笔记(六)——程序刷写
  7. 网络编程-----网络通信协议
  8. 解决网站mp4视频不能边播放边下载的问题!(终极解决方案)
  9. OSChina 周一乱弹 —— 达叔撸猫图还是满满的少女心
  10. 水果之王之猕猴桃-系列五(孕期可以吃猕猴桃吗?)
  11. html鼠标经过状态,HTML5 - 让Canvas内部元素实现鼠标移入、移出效果(Tooltip提示效果)...
  12. 第一天:什么是Flink、WordCount入门、Flink安装、并行度
  13. 替换node-sass为dart-sass
  14. css3 text-shadow 浮雕、镂空、荧光效果
  15. android 动态权限aop,Android — AOP 动态权限申请
  16. Java生鲜电商平台-商品中心的架构设计与源码解析
  17. 阿呆的第一篇CSDN博客
  18. yum安装tomcat
  19. 绝对的内幕!近些年各大IT公司售前薪水
  20. XILINX FPGA K7配置启动流程

热门文章

  1. dbgrideh 为什么只一行_Mysql性能优化:为什么count(*)这么慢?
  2. Java小魔女芭芭拉_沉迷蘑菇不可自拔,黏土人《小魔女学园》苏西·曼芭芭拉 图赏...
  3. alien指令 linux,使用alien命令让deb包和rpm包互相转换
  4. pytorch 命令行运行_PyTorch简介与相关安装
  5. Shell 信号发送与捕捉
  6. JAVA知识基础(二):基本语法
  7. BZOJ4300 绝世好题
  8. 求方程的解 Solve the Equation
  9. 艾拉物联CEO :物联网时代的到来让安全问题显得尤为突出
  10. 2016.04.29-2016.05.05这周工作时间和内容