• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Ruby的25个编程细节(技巧、实用代码段)

Ruby的25个编程细节(技巧、实用代码段)

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了ruby,ruby rose,ruby什么意思,max and ruby,ruby语言等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

1.try 永远不会抛出异常 在 没有的时候 返回 nil
province_id = Province.find_by_name(prov).try(:id) 
</div>

2.find(:first, :condotions) 方法 不言而与
mobile_info = MobileInfo.find(:first, :conditions => ["mobile_num = ? ", mobile_num.to_i]) 
</div>

3.find(:all, :select, :conditions)
support_amount_a = ProvinceMerchantChangeValue.find(:all, :select => "DISTINCT change_value_id", 
                        :conditions => ["status = 1 and merchant_id = ? and province_id =? and channel_id in (select id from channels where status = 1)", 
                        merchant_id, province_id]).map { |cv| cv.change_value_id }.compact 
 
support_amount_s = ChangeValue.find(:all,:select => "price" ,:conditions => ["id in (?)", support_amount_a]) \ 
                                  .map { |cv| cv.try(:price).to_i }.compact 
</div>

4.发送post请求 可以在shell中执行 

curl -d "channel=中信异度支付&action_type=娱人节-手机充值&user_indicate=13911731997&original_amount=10000" http://xx.xxx.xxx:3000/search.json 
</div>

5.Ruby 中纯数据结构 ( Struct 与 OpenStruct )

讲一下它俩之间的区别:

Struct 需要开头明确声明字段; 而 OpenStruct 人如其名, 随时可以添加属性
Struct 性能优秀; 而 OpenStruct 差点, 具体的性能差距可看这里:http://stackoverflow.com/questions/1177594/ruby-struct-vs-openstruct
Struct 是 Ruby 解释器内置, 用 C 实现; OpenStruct 是 Ruby 标准库, Ruby 实现
API 不同: Struct API 与 OpenStruct

6. MIme::Type.register

Mime::Type.register "application/json", :ejson 
config/initializers/mime_types.rb
</div>

7.config/initializers/secure_problem_solved.rb
ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING.delete('symbol') 
ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING.delete('yaml') 
</div>

8.config/initializers/new_rails_default.rb
if defined?(ActiveRecord)
  # Include Active Record class name as root for JSON serialized output.
  ActiveRecord::Base.include_root_in_json = true

  # Store the full class name (including module namespace) in STI type column.
  ActiveRecord::Base.store_full_sti_class = true
end

ActionController::Routing.generate_best_match = false

# Use ISO 8601 format for JSON serialized times and dates.
ActiveSupport.use_standard_json_time_format = true

# Don't escape HTML entities in JSON, leave that for the #json_escape helper.
# if you're including raw json in an HTML page.
ActiveSupport.escape_html_entities_in_json = false
</div>

9.MemCacheStore 缓存
@_cache = ActiveSupport::Cache::MemCacheStore.new(
                      CONFIG['host'], { :namespace => "#{CONFIG['namespace']}::#{@name}" }
                      )

localhost::callback_lock

@_cache.write(pay_channel.channel_id,'true')
v = @_cache.read(pay_channel.channel_id)
if v.nil? || v != 'true'
      return false
    else
      return true
    end
end
</div>

10.联合索引
gem 'composite_primary_keys', '6.0.1'

https://github.com/momoplan
</div>

0.Hash assert_valid_keys 白名单


11.puma -C puma_service_qa.rb

12.pow

13. Time

start_time = start_time.to_s.to_datetime.at_beginning_of_day
end_time = end_time.to_s.to_datetime.end_of_day
</div>

14.merchant.instance_of? MplusMerchant

m_order[:merchant_id] = (merchant.instance_of? MplusMerchant) ? merchant.id : merchant 
</div>

15.will_paginate rails

安装之后需要修改config/environment.rb文件
在文件的最后添加:
require 'will_paginate'
修改controller文件中的index方法:
#    @products = Product.find(:all)
    @products = Product.paginate  :page => params[:page],
           &nbs

您可能想查找下面的文章:

  • ruby迭代map的简洁写法实现原理分析
  • Ruby中访问SQL Server数据库的配置实例
  • ruby声明式语法的实现例子
  • Ruby中的Range对象学习笔记
  • Ruby中的String对象学习笔记
  • Ruby的基本语法学习总结
  • Ruby中的方法(函数)学习总结
  • Ruby中的变量学习总结
  • Ruby数组(Array)学习笔记
  • Ruby和元编程之万物皆为对象

相关文章

  • 你应该知道的Ruby代码风格
  • Ruby中的字符串编写示例
  • CentOS 7下配置Ruby语言开发环境的方法教程
  • 初步讲解Ruby编程中的多线程
  • Ruby中执行Linux shell命令的六种方法详解
  • 源代码快速定位工具-qwandry使用指南
  • Ruby中的变量学习总结
  • web 应用中常用的各种 cache详解
  • Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略
  • Ruby中的集合编写指南

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 剖析 rudy 访问控制
    • 利用RJB在Ruby on Rails中使用Java代码的教程
    • Ruby基本的环境变量设置以及常用解释器命令介绍
    • ruby使用restclient上传服务器本地文件示例
    • ruby写扫描当前网页所有url的脚本
    • 二十分钟 教你Ruby快速入门 图文教程第1/4页
    • 在Ruby中创建和使用哈希的教程
    • ruby基本数据类型简明介绍
    • rails常用数据库查询操作、方法浅析
    • Ruby入门点滴-Ruby的安装

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有