为问题所说的内容编写一个方法,需要找到特定左括号第 n 次出现的索引(由用户定义,即如果用户提供带有附加参数 '{' 和 '5' 的字符串,它将找到这是第五次出现,与“(”和“[”)相同。
目前使用 while 循环来比较每个字符,但这看起来很难看并且不是很有趣,有没有办法用正则表达式来做到这一点?可以在正则表达式中使用变量吗?
def _find_bracket_n(str,left_brac,brackets_num)
i = 0
num_of_left_bracs = 0
while i < str.length && num_of_left_bracs < brackets_num
num_of_left_bracs += 1 if str[i] == left_brac
i += 1
end
n_th_lbrac_index = i - 1
end
Run Code Online (Sandbox Code Playgroud) 此代码应该将单词的索引添加'hello'到indices数组中,但并未将它们添加到数组中:
words = %w(hello how are you then okay then hello how)
def global(arg1, arg2)
indices = []
arg1.each do |x, y|
indices << y if arg2 == x
end
indices
end
global(words,'hello')
#=> [nil, nil]
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题?
在JavaScript中,console.log工作方式如下:
$ node
> console.log(1,2) ; console.log(3,4)
1 2
3 4
Run Code Online (Sandbox Code Playgroud)
在Ruby中,我想输出值a,后跟一个空格,然后输出值,然后输出b换行符.这些都不令人满意:
$ irb
2.4.1 :001 > print 1,2 ; print 3,4
1234 => nil
2.4.1 :002 > puts 1,2 ; puts 3,4
1
2
3
4
=> nil
Run Code Online (Sandbox Code Playgroud)
我没有自己的运气而运气不好吗?
我需要从1最多迭代到给定数字A.我使用以下代码实现了这一点:
(1..A).step(1) do |n| puts n end
Run Code Online (Sandbox Code Playgroud)
有没有比这更好的方法?
我的默认步骤为1.
以下代码中的含义[0]和[1..-1]含义是什么?
def capitalize(string)
puts "#{string[0].upcase}#{string[1..-1]}"
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试case用两个独立的条件写一个声明:
controller_name是'pages'或'users'controller_name存在'static'与action_name存在'homepage'这就是我尝试过的:
case controller_name
when 'pages', 'users'
stylesheet_link_tag "style"
when 'static' && action_name == 'homepage'
stylesheet_link_tag "homepage"
end
Run Code Online (Sandbox Code Playgroud)
第一个when按预期工作,但第二个when没有.我怀疑这&&是导致问题,但我无法弄清楚正确的语法.
我一直在从 JSON 中的 API 中提取数据,并且目前正在遇到一个重要的问题
数据是关于公司的,比如谷歌和 Facebook,并且在一个数组或散列中,如下所示:
[
{"id"=>"1", "properties"=>{"name"=>"Google", "stock_symbol"=>GOOG, "primary_role"=>"company"}},
{"id"=>"2", "properties"=>{"name"=>"Facebook", "stock_symbol"=>FB, "primary_role"=>"company"}}
]
Run Code Online (Sandbox Code Playgroud)
以下是我想尝试的两种操作:
有任何想法吗?
我是 Ruby 的初学者,但是遇到了一些用于数组和哈希的函数/方法(例如未定义的方法)的问题,因为这看起来是一个哈希数组
谢谢!
我有一个ruby数组:
["A", "C", "B", "D", "F", "E"]
Run Code Online (Sandbox Code Playgroud)
用户将提供输入,例如
input = "B"
Run Code Online (Sandbox Code Playgroud)
我想移动数组中的值,因此数组的第一项等于input,并获得新数组的结果:
["B", "D", "F", "E", "A", "C"]
Run Code Online (Sandbox Code Playgroud)
用户将从下拉选项中进行选择,因此他们只能从原始数组中选择字母.
我正在努力替换字符串中的字符,转换"cat"为"hat".
这是我的代码:
str = "cat"
str[0] = 'h'
puts str
Run Code Online (Sandbox Code Playgroud)
但是当我在TryRuby中运行此代码时,我收到此错误:
NoMethodError:未定义的方法`[] ='for"cat"
从用户输入创建由混合数据类型(即包含字符串,整数和浮点数的数组)组成的x个元素组成的数组的最简单方法是什么
到目前为止,我编写了一些使用for循环的代码,但我想知道是否有一种优化它的方法,并且代码行最少。
puts "how many elements?"
max = gets.to_i
array = []
for i in 0..max - 1
puts "are you entering in a string, an int or a float?"
data_type = gets.chomp
if %W[string STRING String s S].include?(data_type)
puts "enter in a string"
array[i] = gets.chomp
elsif %W[int INT Int i I].include?(data_type)
puts "enter an int"
array[i] = gets.to_i
elsif %W[Float FLOAT float f F].include?(data_type)
puts "enter a float"
array[i] = gets.to_f
end
end
print array
Run Code Online (Sandbox Code Playgroud)