ITeye博客

夜鸣猪的Ruby On Rails 空间

夜鸣猪 · 共 628 篇文章

ruby 的回溯 正则表达式

password =~ /^(?=.*\d)(?=.*[a-z])(?=.*[a-z]).{6,40}$/ #(?=.*\d) any thing with a number #at least one number one up case one down case ^ - Matches beginning of line $ - Matches end of line \A - Matches beginning of string. \Z - Matches end of string. If string ends with a newline, it matches just b…

2012-03-03 02:48 · Ruby 1.9 · 评论(2)

动态定义ruby子类,和判断是否定义

上篇有个标准的定义动态类变量 class MyClass class << self attr_accessor :my_constant end def my_method self.class.my_constant = "blah" end end p MyClass.my_constant #=> nil MyClass.new.my_method p MyClass.my_constant #=> "blah" 如果类也动态定义,那么子类和module怎么定义进去的 dynamic_name = "TestEval2" Object.const_set(dynamic_name, …

2012-03-01 04:59 · Ruby 1.9 · 评论(0)

动态定义类变量和类实例变量

class Mine attr_accessor :some_var def intialize @some_var = true end def my_number num self.class.__send__(:attr_accessor, "my_#{num}") self.__send__("my_#{num}=", num) end end dude = Mine.new dude.my_number 1 puts dude.my_1 #=> 1 #or def my_number num self.instance_variable_set "@my_#{num}", num …

2012-03-01 04:52 · Ruby 1.9 · 评论(0)

Eloquent Ruby 读书笔记 续

运算符重载和Ruby风格的运算符重载 程序大概意思是,重新定义,文件类的比较方法。只要目录和文件名一样,就说这两个文件类一样。ruby的 class DocumentPointer attr_reader :folder, :name def initialize( folder, name ) @folder = folder @name = name end def ==(other) return true if other.equal?(self) return false unless other.instance_of?(self.class) folder == other.f…

2012-02-27 23:15 · Ruby 1.9 · 评论(0)

ruby 的 Enumerable 方法

grep 是===来判断是否存在符合条件的sub enumerable c = IO.constants c.grep(/SEEK/) #=> [:seek_set, :seek_cur, :seek_end] res = c.grep(/SEEK/) {|v| IO.const_get(v) } res #=> [0, 1, 2] 用来差方法很不错 array.public_methods.grep(/^each_(.*)$/){ puts $1} #index #with_index #entry #slice #cons #with_object

2012-02-23 03:28 · Ruby 1.9 · 评论(1)

ruby 循环

while(当…) 循环 while 条件 语句1; 语句2 ; 语句… end 单行 while 循环 ( 语句1; 语句2 ; 语句… ) while 条件 until(直到…) 循环 until 条件 = while not (条件) for…in 循环 for 变量 in 对象 语句1; 语句2 ; 语句… end break 跳出当层循环 next 忽略本次循环的剩余部分,开始下一次的循环 redo 重新开始循环,还是从这一次开始 retry 重头开始这个循环体 times 3.times { print "Hi!" } #Hi!Hi!Hi! upto 1.upto(9) {|i|…

2012-02-22 07:26 · Ruby技巧 · 评论(1)

eloquent ruby 读书笔记

节省时间就草记,只记录新鲜,容易忘的 case 的判断是three equal sign, 所以when中可以放正则表达式 set 和 sortset hybrid array hash 三种collection false nil是false所以下面的写法是危险的 if flag == true # do something end #advantage of the expression-oriented nature of Ruby @first_name ||= '' @first_name = '' unless @first_name @first_name = @first_n…

2012-02-22 07:24 · 读书 · Ruby技巧 · 评论(0)

Rails 3 in action 读书笔记

那天看到IE有战友贴了这个书,就想找个时间读读,有收获随笔记下来收获 据说这种格式validate可以多个写一起,不用重复 validates :description, :presence => true, :length => { :minimum => 10 } 这个nested很多地方有了,codeschool railscast和guide project_ticket_path(@project, @ticket) 这个是关联的,就是相当于project_id:integer rails generate model ticket title:string description…

2012-02-11 23:54 · 读书 · Ruby On Rails · 评论(0)

alias_method_chain方法在3.1以后的替代使用方式

alias_method_chain() 是rails里的一个广泛使用的方法,简单说,就是你要重写一个方法,在里面加上一个新方法后,还要使用同名的原方法调用。 使用和实现如下, class Klass def salute_with_log puts "Calling method..." salute_without_log puts "...Method called" end alias_method :salute_without_log, :salute alias_method :salute, :salute_with_log end Klass.new.salute # Pr…

2012-02-04 02:14 · RubyOnRails · Ruby On Rails · 评论(5)

几个git托管服务比较

还听说个gitcafe,不知怎么样 Provider Framework is open-source? Support for other SCM Open-source repositories Space Free private repositories GitEnterprise No No No 1Gb unlimited projects, up to 10 users repo.or.cz Yes No Yes 400M No bettercodes.org Yes SVN Yes 2Gb Yes GitHub No No Yes 300Mb No Codesion No C…

2012-02-02 01:38 · 评论(1)

unbuntu 查看什么占着80端口

sudo lsof -i :80 sudo netstat -lnp | grep :80 你看到结果 tcp6 0 0 :::80 :::* LISTEN 6782/java 然后kill掉6782

2012-01-19 12:37 · Linux · 评论(0)

一些快速解决的问题

问题如下: Could not open library 'libgtkmm-2.4.so': libgtkmm-2.4.so: cannot open shared object file: No such file or directory. 要装 sudo apt-get install libgtkmm-2.4 Invalid gemspec in [/users/john/.rbenv/versions/ree-1.8.7-2011.03/lib/ruby/gems/1.8/specifications/factory_girl_rails-1.5.0.gemspec]: Illf…

2012-01-19 12:35 · RubyOnRails · Ruby On Rails · 评论(0)

脱离rails环境单独使用capybara以及使用capybara测试拖放

require 'rubygems' require 'capybara' require 'capybara/dsl' Capybara.run_server = false Capybara.current_driver = :selenium Capybara.app_host = 'http://www.google.com' module MyCapybaraTest class Test include Capybara def test_google visit('/') end end end t = MyCapybaraTest::Test.new t.test_googl…

2012-01-17 12:49 · Ruby On Rails · Ruby 1.9 · 评论(0)

coffeeScript 的细小差别

$('.drink li a').click (e) -> e.preventDefault() alert $(@).text() 换成 $('.drink li a').click(function(e) { e.preventDefault(); return alert($(this).text()); }); $('.drink li a').click(e) -> e.preventDefault() alert $(@).text() 转成 $('.drink li a').click(e)(function() { e.preventDefault(); return ale…

2012-01-02 04:03 · 评论(1)

2011年辞世的17位科技先驱

美国IT网站eWeek今天撰文,缅怀了今年辞世的17位科技行业先驱。其中,苹果联合创始人史蒂夫·乔布斯(Steve Jobs)位居首位。 以下为文章全文: 除了苹果联合创始人史蒂夫·乔布斯和C语言之父丹尼斯·里奇(Dennis Ritchie)之外,2011年还有许多科技行业的梦想家和技术先驱先后辞世。计算机行业从1950年代开始发展,期间曾有众多卓越的人才为现代化计 算、智能电子和互联网的发展奠定了基础。2011年辞世的很多先驱都曾是科技公司的创始人和CEO,并带领他们所在的企业充分利用了新兴技术的优势。还有 一些人则一直在从事光学、数学和密码学等领域的基础研究。他们很多人都彼此相识,并且…

2011-12-29 05:23 · IT生活 · 评论(0)