草庐IT

nested-map

全部标签

ruby - `map` 是否使用 `each`?

在Ruby中,Enumerable模块混合到集合类中,并依赖于提供each方法的类,该方法产生集合中的每个项目。好吧,如果我想在我自己的类中使用Enumerable,我将只实现each[1]:classColorsincludeEnumerabledefeachyield"red"yield"green"yield"blue"endend>c=Colors.new>c.map{|i|i.reverse}#=>["der","neerg","eulb"]这按预期工作。但是,如果我用Enumerable重写现有类上的each方法,它不会破坏map等函数。为什么不呢?classArrayde

Ruby:如何编写像 map 这样的 bang 方法?

我想编写一些新的Array方法来改变调用对象,如下所示:a=[1,2,3,4]a.map!{|e|e+1}a=[2,3,4,5]...但我对如何执行此操作一无所知。我想我需要一个新的大脑。所以,我想要这样的东西:classArraydefstuff!#changethecallingobjectinsomewayendendmap!只是一个例子,我想写一个全新的,而不使用任何预先存在的!方法。谢谢! 最佳答案 编辑-更新答案以反射(reflect)对您问题的更改。classArraydefstuff!self[0]="a"enden

ruby-on-rails - rails : route helpers for nested resources

我有如下嵌套资源:resources:categoriesdoresources:productsend根据RailsGuides,Youcanalsouseurl_forwithasetofobjects,andRailswillautomaticallydeterminewhichrouteyouwant:Inthiscase,Railswillseethat@magazineisaMagazineand@adisanAdandwillthereforeusethemagazine_ad_pathhelper.Inhelperslikelink_to,youcanspecifyju

ruby - Haml "Illegal Nesting"问题;如何在同一个标​​签中放置多个代码元素?

-@subjects.eachdo|s|%tr%td=s.position%td=s.name%td=s.visible?"Yes":"No"%td=s.pages.size%td=link_to("Show",{:action=>"show",:id=>s.id},:class=>"actionshow")=link_to("Edit",{:action=>"edit",:id=>s.id},:class=>"actionedit")=link_to("Delete",{:action=>"delete",:id=>s.id},:class=>"actiondelete")错误消息:

ruby - 在 ruby​​ 中优雅地实现 'map (+1) list'

title中的短代码是在Haskell中,它做了类似的事情list.map{|x|x+1}ruby。虽然我知道那种方式,但我想知道的是,是否有更优雅的方式来像在Haskell中一样在ruby​​中实现同样的事情。我真的很喜欢ruby​​中的to_proc快捷方式,就像这样:[1,2,3,4].map(&:to_s)[1,2,3,4].inject(&:+)但这只接受Proc和方法之间完全匹配的参数数。我正在尝试寻找一种方法,允许将一个或多个参数额外传递到Proc,而不像第一个演示那样使用无用的临时block/变量。我想这样做:[1,2,3,4].map(&:+(1))ruby是否有类似

ruby-on-rails - Rails 路由 : Nested, 成员、集合、命名空间、范围和可定制

我想了解更多关于Rails路线的信息。成员和收藏#Exampleresourceroutewithoptions:resources:productsdomemberdoget'short'post'toggle'endcollectiondoget'sold'endend命名空间和作用域#Exampleresourceroutewithinanamespace:namespace:admindoresources:productsendscope:admindoresources:productsend约束,Redirect_to#Exampleresourceroutewithop

ruby - Rails map_with_index?

我有以下内容::participants=>item.item_participations.map{|item|{:item_image=>item.user.profile_pic.url(:small)}}我希望这种情况在内部发生的次数不超过3次。我试过map_with_index但没有用。关于在循环中最多运行3次后如何中断的任何建议? 最佳答案 从Ruby1.9开始,您可以使用map.with_index::participants=>item.item_participations.map.with_index{|item

ruby-on-rails - 如何在使用 Ruby on Rails ActiveSupport::Concern 功能时包含模块 "nest"?

我正在使用Ruby1.9.2和RubyonRailsv3.2.2gem。我想“嵌套”模块的包含,因为我正在使用RoRActiveSupport::Concern功能,但我怀疑我应该在哪里声明include方法。也就是说,我有以下内容:moduleMyModuleAextendActiveSupport::Concern#includeMyModuleBincludeddo#includeMyModuleBendend应该我在MyModuleA的“正文”/“上下文”/“范围”中声明includeMyModuleB或者我应该声明包含的do...endblock?有什么区别,我应该从中得到什

ruby - 为什么 6.times.map 在 ruby​​ 1.8.7 而不是 1.8.6 中工作

以下代码片段在MacOSX的1.8.7中运行良好,但在Ubuntu的1.8.6中运行不佳。为什么?有解决方法吗?适用于1.8.7:$ruby--versionruby1.8.7(2009-06-08patchlevel173)[universal-darwin10.0]ltredgate15:eeglleem$irb>>6.times.map{'foo'}=>["foo","foo","foo","foo","foo","foo"]>>但在1.8.6中没有:#ruby--versionruby1.8.6(2008-08-11patchlevel287)[i686-linux]RubyE

ruby-on-rails - Sorcery Gem - 外部提供商的自定义 user_info_mapping

我正在使用SorceryAuthenticationGem的0.7.7版通过NoamB在我的Rails3.2应用程序上我正在寻找一种可能性,如何连接一种为特定外部登录提供商(例如facebook、twitter)执行用户信息映射的方法。例如,我想将提供的语言环境更改为我在数据库中使用的格式,或者我想从Twitter下载用户头像作为匹配过程的一部分。默认情况下,只有通过sorcery.rb文件才能通过这种方式:config.facebook.user_info_mapping={:email=>"email",:first_name=>"first_name",:last_name=>"