How to get the users input and check against the items in an array?

I want the user to enter their style of beard and then I will have it checked against a list of beards in an array of style of beards. This is something extra I wanted to do and i'm not sure how to work it out. The only item I came across to something close to it is the (include?) but I want to have: (array name).include? (value input from user) require_relative 'list_of' require_relati

如何获取用户输入并检查数组中的项目?

我希望用户输入他们的胡须风格,然后我将它与一系列胡须样式的胡须进行核对。 这是我想做的额外事情,我不知道如何解决。 我接触到的唯一项目是(包括?),但我想要: (数组名称).include? (从用户输入的值) require_relative 'list_of' require_relative 'error' #User inputs a value def get_beard puts "What style of beard do you have?" beard = gets if check_beard_ok?(beard) then n

Check if array element appears on line

I'm going through a file line by line and I want to check if that line contains any element from an array. for instance if I have: myArray = ["cat", "dog", "fish"] and the current line said: I love my pet dog The output would say Found a line containing array string Here's what I have but it doesn't work. myArray = ["cat", "dog", "fish"] File.open('file.txt').each_line { |

检查数组元素是否显示在线上

我正逐行阅读文件,我想检查该行是否包含数组中的任何元素。 例如,如果我有: myArray = ["cat", "dog", "fish"] 而现行的说法是: 我爱我的宠物狗 输出会说 找到一个包含数组字符串的行 这是我的,但它不起作用。 myArray = ["cat", "dog", "fish"] File.open('file.txt').each_line { |line| puts "Found a line containing array string" if line =~ myArray #need to fix this logic } 我试过include? 和a

Is there an inverse 'member?' method in ruby?

I often find myself checking if some value belongs to some set. As I understand, people normally use Enumerable#member? for this. end_index = ['.', ','].member?(word[-1]) ? -3 : -2 However, this feels a little less elegant than most of things in Ruby. I'd rather write this code as end_index = word[-1].is_in?('.', ',') ? -3 : -2 but I fail to find such method. Does it even exist? If n

有没有反过来的'会员?' 方法在红宝石?

我经常发现自己在检查某个值是否属于某个集合。 据我所知,人们通常使用Enumerable#成员? 为了这。 end_index = ['.', ','].member?(word[-1]) ? -3 : -2 但是,这比Ruby中的大多数东西感觉不那么优雅。 我宁愿写这个代码 end_index = word[-1].is_in?('.', ',') ? -3 : -2 但我找不到这样的方法。 它甚至存在吗? 如果不是,有什么想法为什么? 不是红宝石,但在ActiveSupport中: characters = ["Konata", "Kagami"

Fastest method to see if all elements in an array have a particular value

I need a very fast way to determine if an array consits only of integers with the value of 9 . Here is my current solution: input = [9,9,9,9,9,9,9,9,9,9,9,9] input.uniq == [9] Can you do it faster? require 'benchmark' n = 50000 Benchmark.bm do |x| x.report do n.times do input = [9,9,9,9,9,9,9,9,9,9,9,9] input.uniq == [9] end end x.report do n.times do in

查看数组中的所有元素是否具有特定值的最快方法

我需要一种非常快速的方法来确定一个数组是否只包含值为9的整数。 这是我目前的解决方案: input = [9,9,9,9,9,9,9,9,9,9,9,9] input.uniq == [9] 你能做得更快吗? require 'benchmark' n = 50000 Benchmark.bm do |x| x.report do n.times do input = [9,9,9,9,9,9,9,9,9,9,9,9] input.uniq == [9] end end x.report do n.times do input = [9,9,9,9,9,9,9,9,9,9,9,9] input.

Match a pattern in an array

There is an array with 2 elements test = ["i am a boy", "i am a girl"] I want to test if a string is found inside the array elements, say: test.include("boy") ==> true test.include("frog") ==> false Can i do it like that? 使用正则表达式。 test = ["i am a boy" , "i am a girl"] test.find { |e| /boy/ =~ e } #=> "i am a boy" test.find { |e| /frog/ =~ e } #=> nil Well you can gr

匹配数组中的模式

有一个包含2个元素的数组 test = ["i am a boy", "i am a girl"] 我想测试一下,如果在数组元素中找到一个字符串,说: test.include("boy") ==> true test.include("frog") ==> false 我可以这样做吗? 使用正则表达式。 test = ["i am a boy" , "i am a girl"] test.find { |e| /boy/ =~ e } #=> "i am a boy" test.find { |e| /frog/ =~ e } #=> nil 那么你可以像这样grep(正则表达式): test.grep

Find value in Array

我如何使用Ruby 1.8.7在Array中找到值? I'm guessing that you're trying to find if a certain value exists inside the array, and if that's the case, you can use Array#include?(value): a = [1,2,3,4,5] a.include?(3) # => true a.include?(9) # => false If you mean something else, check the Ruby Array API Using Array#select will give you an array of elements that meet the criter

在Array中查找值

我如何使用Ruby 1.8.7在Array中找到值? 我猜你正试图找出数组内是否存在某个值,如果是这种情况,可以使用Array#include?(value): a = [1,2,3,4,5] a.include?(3) # => true a.include?(9) # => false 如果你的意思是别的,请检查Ruby Array API 使用Array#select将会给你一个满足条件的元素数组。 但是,如果您正在寻找一种将元素从符合条件的数组中取出的方法,那么Enumerable#detect会是更好的方法: ar

Checking if any element of an array satisfies a condition

Possible Duplicate: check if value exists in array in Ruby I have this method which loops through an array of strings and returns true if any string contains the string 'dog'. It is working, but the multiple return statements look messy. Is there a more eloquent way of doing this? def has_dog?(acct) [acct.title, acct.description, acct.tag].each do |text| return true if text.i

检查数组的任何元素是否满足条件

可能重复: 检查Ruby中数组是否存在值 我有这种方法,它通过一个字符串数组循环,并返回true,如果任何字符串包含字符串'狗'。 它正在工作,但多个返回语句看起来很混乱。 有没有更加雄辩的方式来做到这一点? def has_dog?(acct) [acct.title, acct.description, acct.tag].each do |text| return true if text.include?("dog") end return false end 使用Enumerable#any? def has_dog?(acct) [a

How can I remove RVM (Ruby Version Manager) from my system?

我如何从我的系统中删除RVM(Ruby版本管理器)? There's a simple command built-in that will pull it: rvm implode This will remove the rvm/ directory and all the rubies built within it. In order to remove the final trace of rvm, you need to remove the rvm gem, too: gem uninstall rvm If you've made modifications to your PATH you might want to pull those, too. Check your .bashrc , .profi

我如何从我的系统中删除RVM(Ruby版本管理器)?

我如何从我的系统中删除RVM(Ruby版本管理器)? 有一个简单的内置命令可以实现: rvm implode 这将删除rvm/目录及其内建的所有rvm/ 。 为了移除rvm的最终轨迹,您还需要删除rvm gem: gem uninstall rvm 如果你对PATH进行了修改,你可能也想要这样做。 检查你的.bashrc , .profile和.bash_profile文件等等。 你也可能有一个/etc/rvmrc文件,或者你的主目录~/.rvmrc一个可能需要删除的文件。 如果其他答案不能彻底删

How to create a file in Ruby

I'm trying to create a new file and things don't seem to be working as I expect them too. Here's what I've tried: File.new "out.txt" File.open "out.txt" File.new "out.txt","w" File.open "out.txt","w" According to everything I've read online all of those should work but every single one of them gives me this: ERRNO::ENOENT: No such file or directory - out.txt This happens

如何在Ruby中创建文件

我正在尝试创建一个新文件,而且事情似乎没有像我期望的那样工作。 以下是我所尝试的: File.new "out.txt" File.open "out.txt" File.new "out.txt","w" File.open "out.txt","w" 根据我在网上阅读的所有内容,所有这些都应该可以工作,但是他们中的每一个都给我这个: ERRNO::ENOENT: No such file or directory - out.txt 这发生在IRB以及一个红宝石文件。 我错过了什么? 使用: File.open("out.txt", [your-option-

FizzBuzz doesn't fizz or buzz using case/when

I'm trying to understand how to use a conditional expression in a Ruby case/when statement. This code seems like it should work, but it only prints 1 through 100 inclusive, and never prints "Fizz", "Buzz" or "FizzBuzz". #!/usr/bin/env ruby (1..100).each do |n| case n when n % 3 == 0 && n % 5 == 0 puts "FizzBuzz" when n % 3 == 0 puts "Fizz

FizzBu​​zz不会使用case / when发出嘶嘶声或嗡嗡声

我想了解如何在Ruby case / when语句中使用条件表达式。 这段代码好像应该可以工作,但它只打印1到100,并且从不打印“Fizz”,“Buzz”或“FizzBu​​zz”。 #!/usr/bin/env ruby (1..100).each do |n| case n when n % 3 == 0 && n % 5 == 0 puts "FizzBuzz" when n % 3 == 0 puts "Fizz" when n % 5 == 0 puts "Buzz" else puts n end end 我很确定我错过了一些明显而愚蠢的东西,但文档似乎