jjzjj

immutable-collections

全部标签

java - Ruby 相当于 Java 的 Collections.unmodifiableList 和 Collections.unmodifiableMap

Java的Collections.unmodifiableList和Collections.unmodifiableMap在Ruby标准API中是否有等价物? 最佳答案 使用freeze应用程序接口(interface):Preventsfurthermodificationstoobj.ARuntimeErrorwillberaisedifmodificationisattempted.Thereisnowaytounfreezeafrozenobject.SeealsoObject#frozen?.Thismethodretur

ruby-on-rails - rails 上的 ruby : radio buttons for collection select

我有一个集合选择:此方法的单选按钮是什么?谢谢 最佳答案 Rails3中没有这样的助手。在Rails4中,它是collection_radio_buttons. 关于ruby-on-rails-rails上的ruby:radiobuttonsforcollectionselect,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18525986/

ruby-on-rails - rails : render a collection of models using an specific html view

我有以下关于rails的简单问题。假设我有一个模型用户。在View中,如果我这样做:views/user/_user.html.erb中的文件View将为每个用户调用和打印。如何更改它以使用特定View?我需要这样的东西:User.all:template=>"user/_user_2ndview.html"%>有什么帮助吗?提前致谢 最佳答案 您可以使用collection选项:User.all,:partial=>"users/user2ndview",:as=>:user%>View必须放在views/users/_user2

ruby-on-rails - rails "undefined method for ActiveRecord_Associations_CollectionProxy"

我有这个模型:classStudent如果我在我的movies_controller.rb中写这个:@students=@movie.cinema.companies.students.all我有这个错误:#Company::ActiveRecord_Associations_CollectionProxy:0x00000007f13d88的未定义方法“学生”>如果我这样写:@students=@movie.cinema.companies.find(6).students.all它向我显示了我的select_collection中的正确学生。如何更好地理解这个过程?更新:我需要这部电

ruby-on-rails - ruby rails : How to sort a collection_select

我想按数据库表列“plays”对其进行排序/排序(按我想要的方式降序或升序)我完全糊涂了。刚刚找到了select而不是collection_select的解决方案?我的一些代码不知道如何排序/排序数据库表中还有一些列,如“plays”、“goals”... 最佳答案 只需将实际排序的集合传递给collection_select助手:collection_select(:post,:author_id,Author.order('created_atDESC'),:id,:name_with_initial,:prompt=>true

ruby-on-rails - 如何使用 grouped_collection_select 显示多选?

我正在使用以下代码来显示类别的TreeView选择框:grouped_collection_select(:categories,:category_id,Category.top_level,:children,:name,:id,:name,:include_blank=>true)如何更改它以允许多选?此外,是否可以让它显示复选框而不是选择框? 最佳答案 尝试grouped_collection_select(:categories,:category_id,Category.top_level,:children,:name

ruby-on-rails - Rails 3.2 ActiveAdmin 'Collection is not a paginated scope.' 错误

我正在使用Rails3.2和ActiveAdmin0.4.4开发应用程序。我有一个名为Teaser的模型(/app/models/teaser.rb):classTeasertruemount_uploader:img,TeaserUploaderend然后我向其中添加了ActiveAdmin(/app/admin/teaser.rb):#encoding:UTF-8ActiveAdmin.registerTeaserdoformdo|f|f.inputs"Teaser"dof.input:name,:label=>'Текст'f.input:url,:label=>'Ссылка'

ruby-on-rails - rails/事件记录 : save changes to a model's associated collections

我是否必须保存对模型集合中单个项目的修改,或者是否有一种方法可以在我保存模型时调用以保存它们。#save似乎没有这样做。例如:irb>rental=#...#=>#irb>rental.dvd#=>#,sale_price:#>irb>rental.dvd.copies+=1#=>21irb>rental.save#=>trueirb>rental.dvd#=>#,sale_price:#>irb>Dvd.find_by_title('TheWomenofSummer')#=>#,sale_price:#>在上面的例子中,租赁的DVD副本似乎没有更新数据库中的副本(注意不同的副本数)。

Ruby - 不可变对象(immutable对象)

我有一个用Ruby编写的高度多线程的应用程序,它共享一些实例变量。写入这些变量的情况很少见(1%),而读取很常见(99%)。确保这些线程始终看到所涉及的最新值的最佳方法是什么(无论是您认为还是惯用的Ruby方式)?这是我到目前为止的一些想法(虽然在我彻底修改之前我希望得到你的意见):有一把锁,最常在读取或写入这些变量之前使用(来自JavaConcurrencyinPractice)。这样做的缺点是它在我的代码中放置了很多synchronizeblock,我没有找到避免它的简单方法。使用Ruby的freeze方法(参见here),尽管它看起来同样麻烦并且没有给我任何第一个选项提供的同步优

ruby - 返回 Enumerable 的 Ruby Enumerable.collect 的等价物?

在这段代码中,我创建了一个字符串数组“1”到“10000”:array_of_strings=(1..10000).collect{|i|String(i)}RubyCoreAPI是否提供了一种获取可枚举对象的方法,让我可以枚举同一个列表,按需生成字符串值,而不是生成字符串数组?这是一个进一步的示例,希望能阐明我正在尝试做的事情:deffind_me_an_awesome_usernameawesome_names=(1..1000000).xform{|i|"hacker_"+String(i)}awesome_names.find{|n|notstackoverflow.usere