首页 > 其他分享 >rails 方法小结

rails 方法小结

时间:2023-05-23 13:44:49浏览次数:45  
标签:hash bass 小结 object rails member vocal key 方法

###Rails在Controller中使用helper方法
view_context.link_to "link", "http://www.baidu.com" or ActionController::Base.helpers.link_to "link", "http://www.baidu.com"

  

each_with_object

###old_hash.each_with_object(初期化object) {|item, new_hash| block処理 }


smile = { vocal:'tim', guitar:'brian', bass:'tim', drum:'roger' }

queen = smile.each_with_object({}) do |(key, val), member|
  member[key.to_sym] =
  case key
  when :vocal
    member[:vocal] = 'fleddie'
  when :bass
    member[:bass] = 'jhon'
  else
    val
  end
end

inject

###old_hash.inject(初期化object) {|new_hash, item| block処理 }


smile = { vocal:'tim', guitar:'brian', bass:'tim', drum:'roger' }

queen = smile.inject({}) do |member, (key, val)|
  member[key.to_sym] =
    case key
    when :vocal
      member[:vocal] = 'fleddie'
    when :bass
      member[:bass] = 'jhon'
    else
      val
    end
  member   #injectの場合、ブロックでループする最後に作成中のhashを返す行が必要
end

  

  

 

标签:hash,bass,小结,object,rails,member,vocal,key,方法
From: https://www.cnblogs.com/hello-ruby/p/17424453.html

相关文章

  • ruby on rails 方法小结
    1.获取两个日期之间的月份和年份的唯一数组require'date'defdoit(first,last)first=first<<1(12*last.year+last.month-12*first.year-first.month+1).times.map{|i|(first=first>>1).strftime("%b%Y")}endfirst=Da......
  • Rails:从URL解析路由信息
    ###在Rails3中,您可以执行以下操作:Rails.application.routes.recognize_path"/accounts/1"#{:action=>"show",:controller=>"accounts",:id=>"1"}ActionController::Routing::Routes.recognize_path("/accounts/1&q......
  • .NET中SQL Server数据库连接方法
    1. 使用本机上的SQLServerExpress实例上的用户实例。     用户实例的连接创建了一个新的SQLServer实例。此连接只能是在本地SQLServer2005实例上并且是通过命名管的windows验证连接才有效。目的就是为了给用户创建一个完全权限的SqlServer实例和有限的计算机管理员......
  • git 自己写的使用方法
    QuicksetuporHTTPSSSHWerecommendeveryrepositoryincludea README, LICENSE,and .gitignore.…orcreateanewrepositoryonthecommandlineecho"#Demo">>README.mdgitinitgitaddREADME.......
  • 地理探测器Geodetector的操作方法
      本文介绍Geodetector软件的下载方法,以及地理探测器分析的完整操作,并对其结果加以解读。  首先,我们介绍Geodetector软件的下载方法。进入软件官网,可以看到其中的第四个部分为软件下载区域。对于大多数用户而言,我们后期直接在Excel中运行地理探测器即可(此时Geodetector软件就......
  • 06方法
    目录1.方法概述1.1方法的概念2.方法的定义和调用2.1无参数方法定义和调用2.3无参数方法的练习3.带参数方法定义和调用3.1带参数方法定义和调用3.2形参和实参3.3带参数方法练习4.带返回值方法的定义和调用4.1带返回值方法定义和调用4.2带返回值方法练习14.3带返回值方......
  • Win11右键默认显示更多选项怎么设置 【设置方法】
    转自:https://product.pconline.com.cn/itbk/software/dnyw/1524/15244094.html 怎么让Win11右键默认显示更多选项?有很多朋友不喜欢win11系统的右键菜单显示,经常需要多点一次“显示更多选项”才能看到想要的内容,大家想知道如何让win11右键菜单默认显示更多选项,一下子把所有......
  • strtok() 函数_2种方法的指针实现
    //Lvxin4-1strtok.cpp//strtok()函数的实现2种方法//下面的函数实现考虑一下3种极端情况://"-This,asamplestring"无行尾标志//"-This,asamplestring-"有一个行尾标志//"-This,asamplestring------”有多个行尾标志#define_CRT_SECURE_NO_WAR......
  • strtok() 函数 2种方法的指针实现
    //Lvxin4-1strtok.cpp//strtok()函数的实现2种方法//下面的函数实现考虑一下3种极端情况://"-This,asamplestring"无行尾标志//"-This,asamplestring-"有一个行尾标志//"-This,asamplestring------”有多个行尾标志#define_CRT_SECURE_NO_WAR......
  • python day08 字典、元组、集合内置方法
    字典的内置方法定义方式d={'usernamne':"kevin"}定义空字典:d={}1.key取值dic={'name':'kevin','age':18,'hobbies':['playgame','basketball']}print(dic['name'])#kevinp......