
ruby
文章平均质量分 58
zimo1231
本人勤奋踏实,工作认真负责,自学能力强;性格开朗,容易与人相处,注重团队协作精神,且能承受较大压力。
展开
-
count vs length vs size
In Ruby, #length and #size are synonyms and both do the same thing: they tell you how many elements are in an array or hash. Technically #length is the method and #size is an alias to it.In Active转载 2011-11-08 15:47:12 · 446 阅读 · 0 评论 -
rails 上载xls文件
以下两种form都可以上载文件(http://guides.rails.info/form_helpers.html#what-gets-uploaded) 第一种<%= form_tag({:action => :upload}, :multipart => true) do %> <%= file_field_tag 'picture' %>...2011-12-02 15:09:22 · 109 阅读 · 0 评论 -
ruby中的逻辑运算符
def current_user @current_user ||= session[:user_id] && User.find(session[:user_id])end短短一行代码,却含有很多逻辑,以前老是搞混,这里总结一下。这句代码相当于 def current_user if @current_user return @current...原创 2012-06-11 07:43:46 · 523 阅读 · 0 评论 -
Ruby 之 Block, Proc, Lambda
Block 不是对象,是Ruby的语言特性,近似于闭包(Closure)。范例:def meth res= yield "Block called returns #{res}"endputs meth do next “next_value” end #Block called returns next_valueputs meth do break “...原创 2011-12-06 16:54:23 · 107 阅读 · 0 评论 -
ruby case表达式
Ruby 中的 case 语句非常强大,首先我们来看一个基本用法:grade = case when point >= 85: 'A' when point >= 70 && point < 80: 'B' when point >= 60 && p...原创 2011-12-07 16:27:29 · 296 阅读 · 0 评论 -
ruby字符串处理函数
1.返回字符串的长度str.length => integer2.判断字符串中是否包含另一个串str.include? other_str => true or false "hello".include? "lo" #=> true "hello".include? "ol" #=> false "hello".inc原创 2012-02-28 15:46:02 · 86 阅读 · 0 评论 -
jruby1.6.7.2 integer can't round
在jruby-1.6.7.2中integer无法取round如出现这种问题时需要先对其to_f后再取round原创 2012-07-10 09:32:25 · 109 阅读 · 0 评论 -
Using select, reject, collect, inject and detect.
Looping in Ruby seems to be a process of evolution for newcomers to the language. Newcomers will always find their way to the for loop almost immediately and when confronted with iterating an array...原创 2012-03-08 15:53:20 · 148 阅读 · 0 评论 -
Float round bug in ruby?
ruby-1.8.7 > 1.55.round(1) => 1.6 ruby-1.8.7 > 1.555.round(2) => 1.56 ruby-1.8.7 > 1.155.round(2) => 1.16 ruby-1.8.7 > 10.156.round(2) => 10.16ruby-1.8.7 >...原创 2012-03-24 07:35:56 · 125 阅读 · 0 评论 -
发送ip地址和指定文件到某邮箱
#!/usr/bin/env ruby## ARGV[0] - msg# ARGV[1] - mailto# ARGV[2] - filenamerequire 'open-uri'require 'rubygems'require 'action_mailer'ActionMailer::Base.smtp_settings = { :address...原创 2012-12-03 15:39:33 · 274 阅读 · 0 评论 -
ruby和rails的编程风格
Ruby 社区首推的代码编写风格原文: https://github.com/bbatsov/ruby-style-guide中文翻译:http://ruby-china.org/wiki/coding-styleRuby 社区首推的Rails代码编写风格 原文: https://github.com/JuanitoFatas/rails-style-guide...原创 2012-04-21 09:31:20 · 158 阅读 · 0 评论 -
rails 文件下载功能
controller:class DownController < ApplicationController def down_file send_file "public/files/"+params[:filename] unless params[:filename].blank? end end view...原创 2011-12-01 14:09:11 · 252 阅读 · 0 评论 -
the difference between nil, true, false, blank and empty
The Nil ExpressionIt’s pretty important that you understand the difference of these expressions. To begin, let’s start with nil. Nil is the ruby way of saying NULL. Whenever there is an expression t...原创 2012-06-07 16:20:06 · 139 阅读 · 0 评论 -
Ruby on Rails的神奇
RoR是一个比较神奇的东西,首先建立在一个神奇的语言Ruby之上,有点颠覆我们过去对编程语言的认识(甚至包括一些面向对象的语言),Rails更发扬光大了这一点,其设计者简直是个软件架构的天才,他制定了Rails的“宪法”,使整个开发工作简洁、高效又不失灵活性。如果把编程当成盖房子,Ruby就是盖房子的材料,过去的编程方式是盖一个砖结构的房子,使用的材料是砖块,方式是用砖块堆垒起来;而Ruby就转载 2011-11-10 16:35:36 · 740 阅读 · 0 评论 -
带序号循环Hash
hash.keys.each_with_index do |key, index| value = hash[key] print "key: #{key}, value: #{value}, index: #{index}\n" # use key, value and index as desiredend原创 2011-12-16 09:19:33 · 115 阅读 · 0 评论 -
Ruby学习笔记-循环与选择结构
一、循环结构1. for…in语句:Ruby提供的for...in语句主要用于迭代数组和Hash对象中的元素,与其它语言的for语句有一定的差距,语法格式:for val in Array | Hash | Range [do] #codeend-----------------------------------------...原创 2011-12-16 09:30:06 · 95 阅读 · 0 评论 -
Rubygem 常用命令整理
gem install gem-name # 安装gem,默认会安装最新版本gem uninstall gem-name #卸载gem,如果安装了多个版本会提示要卸载哪个版本gem cleanup #清理无用的gem,这个很有用。当你安装了很多版本的gem,想把旧版本的删除,一个个又太麻烦,就用这个命令gem list #列出所有安装的gemg...原创 2011-12-23 09:00:35 · 537 阅读 · 0 评论 -
Ruby 中获取目录大小
当前 Chito 中统计目录大小完全是偷懒的方式:class Dir def self.size(dir) `du -s #{dir} | awk '{print $1}'`.to_i endend直接调用 du 这个程序然后把结果转换成数值就可以了,非常方便~ 不过要是运行在 ...原创 2012-01-07 22:33:15 · 273 阅读 · 0 评论 -
浅谈Ruby on Rails中的include和extend
从模块引入方法、变量,使得编程变得简单,扩展性愈强,比以往的类的继承更灵活。这样的引入,仿佛将一个方法块,复制了一份放到了你所引用的类或者模块里面。你完全可以将多个互不相干的类中相同的方法拿出来写到一个模块中,这样可以使得代码精简,符合Ruby的设计初衷,而且,使得你的程序更有条理。Ruby on Rails常见用法通常引用模块有以下3种情况:1.在类定义中引入模块,使模...原创 2012-04-23 21:47:55 · 224 阅读 · 0 评论 -
Rubygems 镜像 - 淘宝网
由于国内网络原因(你懂的),导致 rubygems.org 存放在 Amazon S3 上面的资源文件间歇性连接失败。所以你会与遇到 gem install foo 或 bundle install 的时候半天没有响应,具体可以用 gem install rails -V 来查看执行过程。这是一个完整 rubygems.org 镜像,你可以用此代替官方版本,同步频率目前为...原创 2012-01-11 12:21:01 · 100 阅读 · 0 评论 -
六种用ruby调用执行shell命令的方法
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://blackanger.blog.51cto.com/140924/43730碰到需要调用操作系统shell命令的时候,Ruby为我们提供了六种完成任务的方法:1.Exec方法: Kernel#exec方法通过调用指定的命令取代当前进程: 例子...原创 2012-01-12 10:47:02 · 119 阅读 · 0 评论 -
count vs length vs size
In Ruby, #length and #size are synonyms and both do the same thing: they tell you how many elements are in an array or hash. Technically #length is the method and #size is an alias to it.In Act...2011-11-08 15:47:00 · 151 阅读 · 0 评论 -
Ruby on Rails的神奇
RoR是一个比较神奇的东西,首先建立在一个神奇的语言Ruby之上,有点颠覆我们过去对编程语言的认识(甚至包括一些面向对象的语言),Rails更发扬光大了这一点,其设计者简直是个软件架构的天才,他制定了Rails的“宪法”,使整个开发工作简洁、高效又不失灵活性。如果把编程当成盖房子,Ruby就是盖房子的材料,过去的编程方式是盖一个砖结构的房子,使用的材料是砖块,方式是用砖块堆垒起...2011-11-10 16:35:00 · 139 阅读 · 0 评论 -
Ruby Require vs Load vs Include vs Extend
loadload用来多次加载一个库,你必须指定扩展名。 load的使用方法几乎和require一样,但它不会跟踪是否已经加载该库。当你使用一个load方法时,你必须制定“.rb”(扩展库文件名)来加载库,当然它可以多次加载一个库。 很多时候,当你想要用require而不是load,但如果你想每次库调用库都被重新加载那么就使用load。比如,如果你的module经常改变,你...原创 2013-02-18 16:42:22 · 161 阅读 · 0 评论