2012 年文章归档
共 87 篇文章
2种使用class_eval动态定义方法的格式
class MyClass def initialize(dynamic_methods) @arr = Array.new(dynamic_methods) @arr.each{|m| self.class.class_eval do define_method(m) do |*value| puts value end end } end end tmp = MyClass.new %w(method1 method2 method3) tmp.method1 'abc' class MyClass def initialize(dynamic_methods) @arr = Array…
关于method_missing 和respond_to?的基本用法
method_missing is a well-known tool in the Ruby metaprogramming toolbox. It’s callback method you can implement that gets called when a object tries to call a method that’s, well, missing. A well known example of this is ActiveRecord dynamic finders. For example, if your User has an email attribute…
2012-12-12 07:46 · ruby · respond_to? · method_missing
rails 3.1 mysql2 ubuntu
sudo apt-get install libmysql-ruby libmysqlclient-dev
2012-11-22 13:13 · Ruby On Rails
Enabling Pry with reload
For anyone coming to this question recently: the answer has changed in Rails 3.2, because they've changed how they implement reload! Where in earlier version the irb commands were added as methods to Object , now they are added to IRB::ExtendCommandBundle to avoid polluting the global namespace. Wh…
2012-10-23 02:05 · Ruby On Rails
heroku quick deploy
#need pg in Gemfile #sqlite3 in dev 1) gem install heroku 2) heroku login 3) heroku create 4) heroku apps:rename newapp [https://devcenter.heroku.com/articles/renaming-apps] 5) git push heroku master [https://devcenter.heroku.com/articles/git] 6) heroku run rake db:version [https://devcenter.heroku…
2012-10-18 23:07 · Ruby On Rails · Postgresql
ruby 1.9.3-p286 debugger-linecache
update(05/13/2013) rbenv: 解决方案: gem install debugger-linecache -- --with-ruby-include=/home/user/.rbenv/versions/1.9.3-p392 gem install ruby-debug19 -- --with-ruby-include=/home/user/.rbenv/versions/1.9.3-p392 Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. /home/r…
2012-10-18 21:10 · Ruby 1.9
rails 下自定义配置变量
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[rails.env] #rails3 MyApp::Application.config.custom_config_variable = :my_config_setting or Rails.configuration.custom_config_variable = :my_config_setting #need initialization files or the environment.rb YourApp::Application.configure …
2012-10-18 07:36 · Ruby On Rails
MAC 和 Postgresql 第二篇
非常有用的相关资料 UPDATE(05/06/2013): still works fine for MAC modify /usr/local/var/postgres/postgresql.conf get localhost works pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log restart 还是说brew安装,之前之后的事 用brew安装postgresql,很忌讳rvm的gcc,要用xcode的command line brew装完要设置路径,就是local那个不然,就会莫名其妙…
2012-10-16 11:56 · Postgresql
Brew Install Postgresql on OS X Lion
psql: could not connect to server: Permission denied Is the server running locally and accepting connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"? createuser: could not connect to database postgres: could not connect to server: Permission denied Is the server running locally and …
2012-10-12 21:08 · Ruby On Rails
Mac OSX 和 posgresql (2013再次更新版)
UPDATE(2013/06/01) brew services restart postgresql psql -U kenlu -h localhost -d postgres 几个重要说明: mac 下brew安装后要initdb 启动后,默认管理员是系统当前帐户 psql不认的时候,就是brew的路径问题(见下代码) brew装的重启,启动什么的用brew services 在ubuntu下完全不同sudo aptitude 安装后,不许要initdb只要 sudo -u postgres psql template1 template1=# CREATE USER tom WITH…
2012-10-03 05:03 · Ruby On Rails · Postgresql
rspec小抄
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com # defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below) module Player describe MovieList, "with optional description" do it "is pending example, so that you can write ones quickly" it "is already wor…
2012-10-01 21:58
Postgresql 和 rails
update(05/13/2013) ubuntu only ALTER USER postgres with encrypted password 'your_password'; #/etc/postgresql/9.1/main/postgresql.conf #localhost Locate the line #listen_addresses = 'localhost' and change it to: listen_addresses = 'localhost' sudo -u postgres psql template1 \du list user 这里注意ubuntu下…
2012-09-24 02:46 · Ruby On Rails · Postgresql
OpenSSL Errors and Rails – Certificate Verify Failed
Update:11/09/2012 有同鞋求摘要如下: 遇到类似错误,在使用或调用SSL的API时候, RVM怎么用呢 Gem::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError) Gem::RemoteFetcher::FetchError: SSL_connect returned=1 …
2012-09-22 01:29 · Ruby On Rails
查找多文件内容替换
UPDATE: (05/03/2013) 当前使用: #去掉所有的FactoryGirl. find . -type f -name *_spec.rb -print | xargs sed -ie 's/FactoryGirl\.//g' //Seems like Linux is being far too generous. -i 's/^[[:space:]]\{11\}//g' <-- makes it look like "s/^[[:space:]]\{11\}//g" is the extension given to "-i" //baf's quotes serve to…
2012-09-14 20:58 · Linux
shell script
select r.name, count(p.id) from persons as p join rides as r on p.fav_ride_id = r.id group by r.id order by count(p.id) desc limit 2; 一整就用到 $PWD $PATH $HOME The shell has variables to access arguments in the procedures, to define the environment and the like. Shell variables are: $1 # argument ($0.…
2012-09-11 21:08 · Linux
定期清空日志文件
一个关于如何在指定文件大于1GB后,自动删除的问题。 批处理代码如下: #!/bin/bash # 当/var/log/syslog大于1GB时 # 自动将其备份,并清空 # 注意这里awk的使用 if ! [ -f /var/log/syslog ] then echo "file not exist!" exit 1 fi if [ `ls -l /var/log/syslog|awk '{print $5}'` -gt $((1024*1024)) ] then cat /var/log/syslog >> ~/log/history # 将日志备份 echo >> ~/log/his…
2012-09-11 10:04 · Linux
查看一个进程占用了那个端口
有时候需要在Linux下查看一个进程占用了那个端口,但是只知道进程大致的名称,比如要查看hadoop的namenode在哪个端口上运行,以便在eclipse中连接。 首先用ps命令查看进程的id: $ ps -ef | grep Name 其中每一行(很长的时候会占用若干行)的第二个字段就是进程的id。 当然,对于在java虚拟机中运行的进程,比如hadoop的守护进程,可以直接用jsp命令查看: $ jps | grep Name 或者已经知道进程的确切名称,可以用pidof查看: $ pidof Name 查看到进程id之后,使用netstat命令查看其占用的端口: $ netstat …
2012-09-11 09:59 · Linux
说rails文件上传
当前ruby1.9.3 rails 3.2.8,流行的文件上传插件是 paperclip 和 carrierwave 然而,简单的这是需要上传个文件的话,可以用更容易的办法 <%= form_tag({:action => :upload}, :multipart => true) do %> <%= file_field_tag 'picture' %> <% end %> <%= form_for @person do |f| %> <%= f.file_field :picture %> <% end %> def upload uploaded_io = params[:person…
2012-09-10 06:46
postgresql怎么用change.column
def up connection.execute(%q{ alter table contests alter column prize type integer using cast(prize as integer), alter column price set default 200 }) end def up case ActiveRecord::Base.connection when ActiveRecord::ConnectionAdapters::PostgreSQLAdapter connection.execute(%q{ alter table contests a…
2012-09-10 06:33
Ruby里的structure和set
# Create a structure with a name in Struct Struct.new("Customer", :name, :address) #=> Struct::Customer Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main"> # Create a structure named by its constant Customer = Struct.new(:name, :address) #=> Custo…
2012-09-10 06:29 · Ruby On Rails