![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
ruby
okela1985851010
本科毕业,ruby程序员.
展开
-
ruby cookbook -- SortedArray
rubycook book: Recipe 4.7. Making Sure a Sorted Array Stays Sorted [code="ruby"] class SortedArray < Array def initialize(*args,&sort_by) @sort_by = &sort_by || Pro.new{|x,y| x y} sup...原创 2009-12-08 23:54:03 · 74 阅读 · 0 评论 -
irb配置
[code="ruby"] #.irbrc require 'rubygems' require 'hirb' require 'wirble' require 'irb/completion' Wirble.init Wirble.colorize if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAIL...原创 2010-02-24 13:21:14 · 269 阅读 · 0 评论 -
ruby cookbook -- 10.4Getting a Reference to a Method(获得方法引用)
A Method object can be stored in a variable and passed as an argument to other methods. This is useful for passing preexisting methods into callbacks and listeners: 一个Method对象可以存储到变量中并作为参数传递给其他方法。这对...原创 2010-03-01 23:25:49 · 80 阅读 · 0 评论 -
ruby cookbook -- 10.6. Listening for Changes to a Class监听类的变化
当增加新方法,类方法删除和取消定义的现有方法 [code="ruby"] class Tracker def important "This is an important method!" end def self.method_added(sym) if sym == :important raise 'The "impor...原创 2010-03-01 23:30:55 · 89 阅读 · 0 评论 -
ruby cookbook -- 10.7检查对象是否具有必需的属性
检查是否具有实例变量 [code="ruby"] class Object def must_have_instance_variables(*args) vars = instance_variables.inject({}) { |h,var| h[var] = true; h } args.each do |var| unless vars[...2010-03-01 23:51:26 · 90 阅读 · 0 评论 -
ruby的笔记
最近看了看ruby元编程的一些东西。简单的记下。 1,ruby的class_eval。打开类上下文,定义类的实例方法。 attr_accessor应该就是一个类方法,然后打开类上下文,定义一堆get,set方法。 2,rails回调大量使用了alias_method_chains实现。 3,ruby的类变量和类的实例变量区别。父类的类变量和子类的类变量是同一个,类实例变量不是。 4,bl...原创 2010-05-20 14:23:44 · 82 阅读 · 0 评论 -
Benchmark
1,length > 0和blank?和emtpy? [code="java"]>> Benchmark.bm do |x| ?> x.report{10000.times{[].blank?}} >> x.report{10000.times{[].empty?}} >> x.report{10000.times{[].length > 0}} >> end use...2010-06-17 14:10:24 · 93 阅读 · 0 评论 -
关于浮点数精度的问题
在项目里遇到一个很诡异的问题,因为有一些浮点数的计算,总是失败,所有将出问题的变量打印到日志里。 结果如下: [quote] ====reicpe_blocked 1.9 ====real_block 1.9[/quote] 是这样的,recipe_blocked变量值是:1.9,real_block变量值是1.9,两个结果相减结果是result,但是在程序对...原创 2011-05-11 15:50:58 · 199 阅读 · 0 评论 -
Regular Expressions
Regular expressions (“regexps”) match strings. /abc/ =~ "abc" When a match is successful, the return value 0 ֒→ ...原创 2010-10-16 22:55:01 · 113 阅读 · 0 评论 -
Nesting Is Different From Inclusion
Nesting Is Different From Inclusion [color=red]An internal module has access to all the names in its “parent” module.[/color] Here’s an example: irb(main):017:0> module NewModule ...原创 2010-10-17 10:02:55 · 87 阅读 · 0 评论 -
ruby的to_proc
1,先看api [quote]Method#proc meth.to_proc => prc Returns a Proc object corresponding to this method. prc.to_proc → prc Part of the protocol for converting objects to Proc objects. Instances of cl...原创 2010-10-21 00:41:43 · 168 阅读 · 0 评论 -
included() vs extended()
[code="ruby"]# A little helper from _why class Object def metaclass class [Hooks, Class, Module, Object, Kernel] Bar.metaclass.ancestors #=> [Hooks, Class, Module, Object, Kernel][/code] ...原创 2010-11-04 19:48:34 · 92 阅读 · 0 评论 -
json
[url]http://flori.github.com/json/[/url]原创 2011-09-15 09:51:32 · 58 阅读 · 0 评论 -
RVM Install
[url]http://beginrescueend.com/[/url]原创 2011-09-17 15:17:02 · 73 阅读 · 0 评论 -
Ruby Memoization(转载)
转载[url]http://fuliang.iteye.com/blog/827321[/url] [code="ruby"] module Memoizable def memoize( name, cache = Hash.new ) original = "__unmemoized_#{name}__" ([Class, Module].in...原创 2010-11-28 23:45:20 · 98 阅读 · 0 评论 -
Metaprogramming Ruby
P30 In a sense, the class keyword in Ruby is more like a scope operator than a class declaration.原创 2011-09-30 16:11:33 · 97 阅读 · 0 评论 -
Rails计算某月最后一天
经常忘记这个函数.mark下. [quote]end_of_day, end_of_month, end_of_quarter, end_of_week, end_of_year[/quote]原创 2011-08-12 10:46:56 · 218 阅读 · 0 评论 -
ActiveRecord::Dirty
[quote]Track unsaved attribute changes. A newly instantiated object is unchanged:[/quote] [code="ruby"] a=ManufacturingElement.new >> a.changed? => false >> a.uom= => "m" >> a.chan => tr...原创 2011-11-21 10:29:20 · 103 阅读 · 0 评论 -
ruby cookbook -- 使分配程序能够使用注册回调的返回值
#使分配程序能够使用注册回调的返回值 A simple change to the EventDispatcher class makes it possible for the dispatcher to use the return values of the registered callbacks. The original implementation of EventDispa...2010-02-23 19:23:10 · 61 阅读 · 0 评论 -
ruby cookbook -- 使用回掉的松偶合系统
7.11 使用回掉的松偶合系统 Coupling Systems Loosely with Callbacks Here's a mixin module that gives each instance of a class its own hash of "listener" callback blocks. An outside object can listen for a parti...2010-02-23 19:20:28 · 82 阅读 · 0 评论 -
闭包(回顾,转载)
闭包的一个重要特征是:过程(方法)内部定义的变量,即使在方法调用完成以后,仍然可以继续引用到!(即延长了生命周期) [code="ruby"] def method(n) puts "n=#{n}"; #测试用 return proc{|i| n +=i } #关键:proc是一个过程对象,只能由"委托"用call来调用,这里在过程内部引用了参数变量n,将会在多次call该过程后,保...2010-03-22 23:02:38 · 72 阅读 · 0 评论 -
rdoc
之前在安装ruby的时候, 因为用源码编译装的ruby,所以rodc没有装上。 而后有用sudo apt-get install rdoc装了rdoc, 发现总是找不库,我想应该用这种自动装的不会找到我手动装的库, 除非ruby也是自动装的, 而后到新得立软件管理查看,发现没有找到ruby,汗啊。 sudo apt-get auto-remove rdoc卸载掉rdoc。 再找到源码...原创 2009-07-20 23:20:48 · 140 阅读 · 0 评论 -
proc和yield的区别
最近开始学习ruby,总是搞不懂proc和yield的区别。 今天有仔细翻了下资料,学习了下,感觉有点明了了。 对于proc和yield总结如下: yield,意味让路,就是这里要写代码但是要写什么还不知道,那先yield。等以后想好了再写上;proc是先写了一段代码,但是暂时不用,我先放着,等到想用了在call你。 关于这方面的例子网上可以找到很多。附一个别人的供大家参考。 [code...原创 2009-07-26 13:42:56 · 221 阅读 · 0 评论 -
Array的*方法
今天在做一个ruby的小练习。 最后我想得到的结果是这样的,对于数组a=[1,2,3],我想让他输出1*2*3。 开始用了一个这样的方法,a.each{|s| print s,'*'},得到的的结果是1*2*3*。多了一个加,然后我有翻看了下Array发现一个这样的方法Array#each_index,可以传递数组的下标到block,然后在实现。写了下还是不满意,觉得这么简单的东西...原创 2009-07-30 00:39:27 · 221 阅读 · 0 评论 -
requir和load的区别
不同点: Load: 1、每次执行load的时候,它都会无条件的加载指定的ruby的源文件。 2、使用它可以重新来加载一个在程序开始运行后可能修改的源文件。 require 1、加载指定的文件,且只加载一次 2、require可以加载二进制库 3、require是一条可执行语句-它可能在一个if语句内...原创 2009-08-20 16:16:45 · 243 阅读 · 0 评论 -
ruby里的方法作用域
在java里private方法在Java当中的含义是只在当前类范围之内可以调用,当前类的子类也无法访问,但能被同一类别的其他对象实例调用.protected方法在Java当中的含义是在当前类所在的整个package当中都可见,其他package类则无法访问; ruby里private,protected都可以被子类继承,但是都不能被实例调用. protected可以被同类的(含子类)的其他...原创 2010-08-11 09:51:54 · 107 阅读 · 0 评论 -
ruby cookbook -- Making a Copy of an Object
[b]Recipe 8.16. Making a Copy of an Object[/b] [color=darkred]The downside of dup is that it creates a new instance of the object's original class.[/color] If you open up a specific object and give i...原创 2010-02-20 22:51:05 · 88 阅读 · 0 评论 -
ruby cookbook -- Hash
Hash 1. Except for strings and other built-in objects, most objects have a hash code equivalent to their internal object ID. As seen above, you can override Object#hash to change this, but the onl...原创 2010-02-23 18:40:54 · 62 阅读 · 0 评论 -
ruby cookbook -- Creating a Hash with a Default Value
Creating a Hash with a Default Value [code="ruby"] first_letter_hash = Hash.new { |hash, key| hash[key] = [] } text.split(/\W+/).each { |word| first_letter_hash[word[0,1].downcase] {"m"=>["mainly"...原创 2010-02-23 18:45:42 · 58 阅读 · 0 评论 -
ruby cookbook -- Using an Array or Other Modifiable Object as a Hash
Recipe 5.5. Using an Array or Other Modifiable Object as a Hash Key Suppose you want a reliably hashable Array class. If you want this behavior universally, you can reopen the Array class and redefin...2010-02-23 18:51:24 · 100 阅读 · 0 评论 -
ruby cookbook -- Validating an Email Address
Recipe 1.19. Validating an Email Address === Discussion Most email address validation is done with naive regular expressions like the ones given above. Unfortunately, these regular expressions ar...原创 2010-02-23 18:53:01 · 137 阅读 · 0 评论 -
ruby cookbook -- Choosing Randomly from a Weighted List
Recipe 5.11. Choosing Randomly from a Weighted List Store the elements in a hash, mapped to their relative probabilities. The following code will work with a hash whose keys are mapped to relative in...2010-02-23 18:58:29 · 59 阅读 · 0 评论 -
ruby cookbook -- Files and Directories
Chapter 6. Files and Directories 创建目录结构的代码 [code="ruby"] # create_tree.rb def create_tree(directories, parent=".") directories.each_pair do |dir, files| path = File.join(parent, dir) ...2010-02-23 19:01:11 · 62 阅读 · 0 评论 -
ruby cookbook -- 备份至带版本号的文件名
6.14备份至带版本号的文件名。 Problem You want to copy a file to a numbered backup before overwriting the original file. More generally: rather than overwriting an existing file, you want to use a new file who...2010-02-23 19:04:44 · 80 阅读 · 0 评论 -
ruby cookbook -- Enumerable mixin - good example of Ruby's duck typing
Similarly, File and String both include the Enumerable mixin, so in a lot of cases you can read from an object without caring what type it is. This is a good example of Ruby's duck typing. [code="ru...2010-02-23 19:09:30 · 88 阅读 · 0 评论 -
ruby cookbook -- 使用find,来查找匹配的文件
ruby操作文件,使用find,来查找匹配的文件 6.20 Use the Find.find method to walk the directory structure and accumulate a list of matching files. Pass in a block to the following method and it'll walk a directory...2010-02-23 19:11:45 · 161 阅读 · 0 评论 -
ruby cookbook -- Looping Through Multiple Iterables in Parallel
Recipe 7.9. Looping Through Multiple Iterables in Parallel Any object that implements the each method can be wrapped in a Generator object. If you've used Java, think of a Generator as being like a J...2010-02-23 19:13:26 · 69 阅读 · 0 评论 -
Ruby 搭建环境
[url]http://kidlet.sinaapp.com/blog/archives/185.html[/url] [url]http://kidlet.sinaapp.com/blog/archives/205.html[/url] 因为想装RVM 所以要对上述程序进行删除,其中 Ruby 和 RubyGem 均是采用从官方下载源代码进行编译安装的 结果发现 官方提供的 M...原创 2013-06-01 11:17:00 · 120 阅读 · 0 评论