jjzjj

value-in-array

全部标签

ruby - 用于 Ruby 哈希的 map_values()?

我想念Ruby中的Hash方法来仅转换/映射散列值。h={1=>[9,2,3,4],2=>[6],3=>[5,7,1]}h.map_values{|v|v.size}#=>{1=>4,2=>1,3=>3}你如何在Ruby中归档它?更新:我正在寻找map_values()的实现。#moreexamplesh.map_values{|v|v.reduce(0,:+)}#=>{1=>18,2=>6,3=>13}h.map_values(&:min)#=>{1=>2,2=>6,3=>1} 最佳答案 Ruby2.4引入了方法Hash#tran

ruby - 重构 Ruby : Converting string array to int array

我正在重构一个西洋跳棋程序,我正在尝试将玩家移动请求(例如以“3、3、5、5”的形式)处理到一个int数组中。我有以下方法,但感觉不像我所知道的那样像Ruby:deftranslate_move_request_to_coordinates(move_request)return_array=[]coords_array=move_request.chomp.split(',')coords_array.each_with_indexdo|i,x|return_array[x]=i.to_iendreturn_arrayend我用它进行了以下RSpec测试。it"translatesa

ruby - "string literal in condition"是什么意思?

每当我尝试运行该程序时,都会弹出一条错误消息“条件字符串文字(第10行)”。我做错了什么?puts"Welcometothebestcalculatorthereis.Wouldyouliketo(a)calculatetheareaofageometricshapeor(b)calculatetheequationofaparabola?Pleaseenteran'a'ora'b'togetstarted."response=gets.chompifresponse=="a"or"A"puts"ok."elsifresponse=="b"or"B"puts"awesome."else

arrays - 在 Ruby 中将字符串转换为 int 数组

如何将字符串:s='23534'转换为这样的数组:a=[2,3,5,3,4]有没有一种方法可以遍历ruby​​中的字符并将它们中的每一个转换为to_i或者甚至像Java一样将字符串表示为char数组,然后转换所有字符to_i如您所见,我在字符串中没有这样的分隔符,,我在SO上找到的所有其他答案都包含一个分隔符。 最佳答案 一个简单的衬里是:s.each_char.map(&:to_i)#=>[2,3,5,3,4]如果您希望在字符串不只包含整数时显示错误,您可以这样做:s.each_char.map{|c|Integer(c)}这会引

ruby-on-rails - Rails 和 MQTT : Subscribe to topic in background at server startup?

我想在服务器启动时在我的Rails应用程序中订阅一个mqtt主题,并保持订阅始终处于事件状态和运行状态。我正在使用这个mqttgem进行mqtt通信:https://github.com/njh/ruby-mqtt这是我现在拥有的:在application.rb中:config.after_initializedomqttSub=BackgroundMQTT.newmqttSub.runend后台MQTT类:classMQTTSubscriberdefrunThread.newdoMQTT::Client.connect(:host=>'localhost',:port=>1883,)

ruby Mechanize : multiline value for textarea gets merged

编辑:经过进一步测试,问题似乎是站点特定的,理论上应该可以正常工作。本应位于多行的Textarea值正在一行中全部提交。textarea_values="value1\nvalue2"form=page.form_with(:id=>'form_id_here')form['my_textarea']=textarea_valuessubmit=form.button_with(:value=>'Submit')form.click_button(submit)提交的值是value1\nvalue2而不是预期的多行。有没有我可以尝试的另一种添加表单值的方法?

ruby-on-rails - 脚本/服务器 custom_require.rb :36:in `require' : cannot load such file -- test/unit/error (LoadError)

我有一个基于1.8.7构建的应用程序,我正尝试在1.9.3的系统上启动它当我运行脚本/服务器时,我得到:/usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in`require':cannotloadsuchfile--test/unit/error(LoadError)from/usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in`require'我的服务器脚本如下所示:#!/usr/bin/envrubyrequireFile.expand_path('../.

ruby - 运行时间 Array#unshift 与 Array#shift

我预计Array#shift和Array#unshift的运行时间都是Θ(n)。原因是机器需要遍历每个数组成员并将其分配给左侧或右侧的键。在Array#unshift的情况下,假设只有一个值作为参数传入并且有很多数组成员,我假设array[0]对运行时间没有显着影响。换句话说,当数组成员的数量很高而传递给Array#unshift的变量数量很少时,我期望Array#shift和Array#unshift以获得相同的运行时间。在Ruby2.1.2上运行基准测试时,这些假设不成立。为什么?代码:require'benchmark'GC.disablenumber_of_elements=2

ruby-on-rails - Rails 设计 : "You need to sign in or sign up before continuing" instead of "You will receive an email with instructions.."

我已经安装了DeviseonRails4.2.0,一切似乎都在工作,我使用了以下指南:http://sourcey.com/rails-4-omniauth-using-devise-with-twitter-facebook-and-linkedin/我的设计模块是:devise:database_authenticatable,:registerable,:confirmable,:recoverable,:rememberable,:trackable,:validatable,:omniauthable唯一的问题是,如果我尝试通过转到注册页面创建一个新帐户,然后在输入我的电子邮

ruby - 为什么 Array 不覆盖 Ruby 中的三重等号方法?

我刚刚注意到Array没有覆盖三重等号方法===,这有时被称为大小写相等方法。x=2casexwhen[1,2,3]then"match"else"nomatch"end#=>"nomatch"而范围运算符则:x=2casexwhen1..3then"match"else"nomatch"end#=>"match"但是,您可以为数组做一个变通办法:x=2casexwhen*[1,2,3]then"match"else"nomatch"end#=>"match"知道为什么会这样吗?是不是因为x更可能是一个实际的数组而不是一个范围,并且数组覆盖===意味着普通的相等性不会匹配?#Thisi