Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 迭代以特定字母开头的名称数组_Arrays_Ruby_String_Methods - Fatal编程技术网

Arrays 迭代以特定字母开头的名称数组

Arrays 迭代以特定字母开头的名称数组,arrays,ruby,string,methods,Arrays,Ruby,String,Methods,与这一概念作斗争 例如: names = ["Steve", "Mason", "John", "Sarah"] 如果我想使用each方法只为姓名以字母“S”开头的人输出一些文本,我该如何做 pets = ["Scooby", "Soco", "Summer", "Pixie", "Wilson", "Mason","Baron", "Brinkley", "Bella"] (1..9).each {|pets| def start_with? if pets.start_w

与这一概念作斗争

例如:

names = ["Steve", "Mason", "John", "Sarah"]
如果我想使用each方法只为姓名以字母“S”开头的人输出一些文本,我该如何做

pets = ["Scooby", "Soco", "Summer", "Pixie", "Wilson", "Mason","Baron", "Brinkley", "Bella"] 
(1..9).each {|pets| 
  def start_with? 
    if pets.start_with? "S"
      puts "My name starts with an S for super!" 
    else 
     puts "I’m still pretty special too!" 
    end 
  end
 }
您需要一个:

如果第一个字母是“S”,则仅打印
名称
(通过
放置
)。如果您不必使用
每个
,则可以执行以下操作:

puts names.grep(/\AS/)
您需要一个:

如果第一个字母是“S”,则仅打印
名称
(通过
放置
)。如果您不必使用
每个
,则可以执行以下操作:

puts names.grep(/\AS/)

使用
每个
的基本方法:

names = ['Steve', 'Mason', 'John', 'Sarah']
names.each do |name|
  puts 'some text' if name.start_with?('S')
end
您可以阅读有关
每一个
开始的更多信息


(可能有更快的方法来确定字符串是否以单个字符开头,但我喜欢这种方法是非常自我记录的。)

使用
每个
的基本方法:

names = ['Steve', 'Mason', 'John', 'Sarah']
names.each do |name|
  puts 'some text' if name.start_with?('S')
end
您可以阅读有关
每一个
开始的更多信息


(可能有更快的方法来确定字符串是否以单个字符开头,但我喜欢这种方法是非常自我记录的。)

我们可以通过使用正则表达式来解决这个问题:

names = ["Steve", "Mason", "John", "Sarah"]
names.each do |name| 
  puts name if name =~ /^s/ 
end

我们可以通过使用正则表达式来解决这个问题:

names = ["Steve", "Mason", "John", "Sarah"]
names.each do |name| 
  puts name if name =~ /^s/ 
end
您也可以手动检查每个宠物名字的第一个字母,而不是使用

def pets_that_start_with_s(pets_array)
  pets_array.each do |pet|
    if pet[0].upcase == 'S'
      puts "My name is #{pet}, it starts with an S for Super!" 
    else 
      puts "My name is #{pet}, I’m still pretty special too!" 
    end
  end
end

pets = ["Scooby", "Soco", "Summer", "Pixie", "Wilson", "Mason","Baron", "Brinkley", "Bella"]

pets_that_start_with_s(pets)
输出:

My name is Scooby, it starts with an S for Super!
My name is Soco, it starts with an S for Super!
My name is Summer, it starts with an S for Super!
My name is Pixie, I’m still pretty special too!
My name is Wilson, I’m still pretty special too!
My name is Mason, I’m still pretty special too!
My name is Baron, I’m still pretty special too!
My name is Brinkley, I’m still pretty special too!
My name is Bella, I’m still pretty special too!
N.B.已添加,以确保宠物名的大小写没有问题。

您也可以手动检查每个宠物名的第一个字母,而不是使用

def pets_that_start_with_s(pets_array)
  pets_array.each do |pet|
    if pet[0].upcase == 'S'
      puts "My name is #{pet}, it starts with an S for Super!" 
    else 
      puts "My name is #{pet}, I’m still pretty special too!" 
    end
  end
end

pets = ["Scooby", "Soco", "Summer", "Pixie", "Wilson", "Mason","Baron", "Brinkley", "Bella"]

pets_that_start_with_s(pets)
输出:

My name is Scooby, it starts with an S for Super!
My name is Soco, it starts with an S for Super!
My name is Summer, it starts with an S for Super!
My name is Pixie, I’m still pretty special too!
My name is Wilson, I’m still pretty special too!
My name is Mason, I’m still pretty special too!
My name is Baron, I’m still pretty special too!
My name is Brinkley, I’m still pretty special too!
My name is Bella, I’m still pretty special too!

N.B.已经添加,以确保宠物名字的大写没有问题。

您能展示一下您迄今为止都做了哪些尝试吗?pets=[“史酷比”、“索科”、“夏天”、“小精灵”、“威尔逊”、“梅森”、“男爵”、“布林克利”、“贝拉”](1..9)。每个{pets | def以?if pets开头。以?S开头“我的名字以S开头,代表超级!”还有人说“我也很特别!”保持你的问题简洁明了。没有人担心你的过期时间,我们都在这里,我们都在学习。你在循环中一次又一次地定义同一个方法。你甚至从来没有调用过你定义的方法。不清楚代码应该实现什么。你能展示一下你到目前为止尝试了什么吗?pets=[“Scooby?”“,”Soco“,”Summer“,”Pixie“,”Wilson“,”Mason“,”Baron“,”Brinkley“,”Bella“](1..9)。每个{宠物| def start|u以?if pets.start|u以?“S”开头的单词是“我的名字以S开头表示超级!”,其他单词是“我也很特别!”结尾}保持你的问题简洁明了。没有人担心你的死亡,我们都在这里,我们都在学习。您在循环中一次又一次地定义相同的方法。您甚至从未调用您定义的方法。不清楚该代码应该实现什么。你是指
/\AS/
还是
/\AS/i
<代码>^
在Ruby中是行的开头,
\A
是字符串的开头;你需要注意你的
S
的大小写。你是说
/\AS/
还是
/\AS/i
<代码>^
在Ruby中是行的开头,
\A
是字符串的开头;您需要关注您的
S
。您好,谢谢您的回复。这对我有用。如何像您刚才那样提交代码块?我似乎在评论中找不到该怎么做。我想让你看看我做了什么。嗨,谢谢你的回复。这对我有用。如何像您刚才那样提交代码块?我似乎在评论中找不到该怎么做。我想让你看看我做了什么。