The Power of Variable Names
- 以问题为导向(Problem Orientation)
- 一个好的名字表达的是what而不是how
- 最适当的名字长度:8-20
solution but needs your judgment
class User < ActiveRecord::Base
has_many :memberships def find_recent_active_memberships
memberships.only_active.order_by_activity.limit(5) end end
class Membership < ActiveRecord::Base belongs_to :user scope :only_active, where(:active => true) scope :order_by_activity, order('last_active_on DESC') end
<ul> <% User.find(:order => "last_name").each do |user| -%> <li><%= user.last_name %> <%= user.first_name %></li> <% end %> </ul>
===========>
class UsersController < ApplicationController def index @users = User.order("last_name") end end
<ul> <% @users.each do |user| -%> <li><%= user.last_name %> <%= user.first_name %></li> <% end %> </ul>===========>
class UsersController < ApplicationController def index @users = User.ordered end end
class User < ActiveRecord::Base
def self.ordered order("last_name") end end
Solution class User < ActiveRecord::Base scope :ordered, order(“last_name”) end
class Address < ActiveRecord::Base
belongs_to :customer end
class Customer < ActiveRecord::Base has_one :address has_many :invoices end
class Invoice < ActiveRecord::Base
belongs_to :customer end
# Views
<%= @invoice.customer.name %> <%= @invoice.customer.address.street %>
<%= @invoice.customer.address.city %>
<%= @invoice.customer.address.state %>
<%= @invoice.customer.address.zip_code %>
Law of Demeter: lays out the concept that an object can call methods on a related object but that it should not reach through that object to call a method on a third. In Rails, this could be summed up as “use only one dot.”
So we can write a method :
class Customer < ActiveRecord::Base
has_one :address has_many :invoices def street address.street end def city
address.city end def state address.state end
end
<%= @invoice.customer_name %>
<%= @invoice.customer_street %>
In addition, your public interface on Invoice has been polluted by methods that arguably have nothing to do with the rest of your interface for invoices.
Solution
Fortunately, Ruby on Rails includes a function that addresses the first concern. This method is the class-level delegate method. This method provides a shortcut for indicating that one or more methods that will be created on your object are actually provided by a related object. Using this delegate method, you can rewrite your exam- ple like this:
class Customer < ActiveRecord::Base
has_one :address has_many :invoices delegate :street, :city, :state, :zip_code, :to => :address
end
class Invoice < ActiveRecord::Base belongs_to :customer delegate :name, :street, :to => :customer, :prefix => true
end
面试时发现了自己的很多不足,从今天起再次系统的学习一下Ruby
rails 3 中新增加了一个Library管理工具 Bundler,它提供了好的工具来管理rails3项目依赖的gems。我们需要通过Gemfile来管理requires.
下面是Gemfile的一般写法:
每个Gemfile至少要依赖于一个以上的源,通过以下的形式进行声明:
source :rubygems
source "http://rubygems.org"
source :rubyforge
source "http://gems.rubyforge.org"
source :gemcutter
source "http://gemcutter.org"
当然的由于众所周知的墙的问题,使用上边的源可能会出现某些问题…我推荐使用淘宝提供的墙内源:
source 'http://ruby.taobao.org'
通过gem关键字来声明所依赖的Gems
gem "nokogiri"
gem "rails", "3.0.0.beta3"
gem "rack", ">=1.0"
gem "thin", "~>1.1"
第一个参数是gem的名字,第二个参数可以指定版本,后边说明符的用法如下
关于Gems的详细介绍可以参见这里
如果 require 的档名不同,可以加上 :require 参数
gem "rspec", :require => "spec"
gem "sqlite3-ruby", "1.2.5", :require => "sqlite3"
gem "gem-release", :require => false
gem "redis", :require => ["redis/connection/hiredis", "redis"]
更神奇的是还可以用 Git 当做来源,甚至可以指定 branch, tag 或 ref,当然了默认的 branch 是 master 啦。
gem "nokogiri", :git => "git://github.com/tenderlove/nokogiri.git", :branch => "1.4" git "git://github.com/wycats/thor.git", :tag => "v0.13.4"
gem "thor"
如果你是一个Gem的开发者,你还可以直接指定本地的gem哦
gem "nokogiri", :path => "~/sw/gems/nokogiri"
最后还要介绍一个比较重要的关键字 Group,可以指定某些Gem在特定的环境下被加载:
gem "wirble", :group => :development
gem "ruby-debug", :group => [:development, :test]
group :test do
gem "rspec"
end
没有指定 Group 的Gems是属于 Default Group 的。 通过 Group 我们可以更方便的执行 Bundle ,比如
$ bundle install --without test development
Group的功能还是很强大的,大多数特性咱也不太会用的说,这里有老外写的某文章,可以看看啦。
直接执行$ bundle
可以列出常用的命令,比如
bundle check # 检查目前缺少哪些Gems
bundle init # 在当前目录生成Gemfile
bundle install # 根据Gemfile安装环境到当前的系统
关于 RubyGems 我觉得没理由不看官方文档的吧,各个方面都特别详细,有时间好好看看.
今天的这篇主要参考的Ruby-China的这篇帖子,基本是照搬…Orz
$ rails new appname --skip-test-unit --skip-bundle
OS X 要安装 growl-notify ,是这里还是这里,就看你了。 liveload的浏览器扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
$ rails g rspec:install
$ guard init
$ bin/guard init rspec && bin/guard init spork
修改 Guardfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
如果你没有使用activerecorder 一定要按注释修改文件.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
$ bundle exec spork --bootstrap
$ guard
就可以了去年8月份开始接触RoR,之后虽然做过几个小的App但是并没有对Rails进行一个系统的学习,翻看之前的代码,简直惨不忍睹,并且带有浓重的C# Style,今天捧起“Ruby on Rails 3 Tutorial”开始从头学起,从今以后将作为一个Rubyist开始新的生活。
通过git-osx-installer进行安装,异常的简单,简单的使用教程可以看这里。
之后要下载并安装Xcode,当然了主要是Command Line Tools for Xcode。
这里先推荐的一个东西homebrew,Mac OS X 下新的软件包管理工具,比较方便的,之后就可以使用类似$ brew install unrar
的命令管理包了,安装方法:
/usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
也可以按照这篇文章中的做法进行安装:
首先,Homebrew 的原则是“No sudo”,也就是说,既然 Mac OS X (client 版本) 绝大部分情况下都是归你这个有管理员权限的用户,为什么在自己的 /usr/local 下安装程序还需要 sudo 呢?所以,首先:
$ sudo chown -R \`whoami` /usr/local
然后可以正式开始安装,我推荐的安装方式是先用 git-osx-installer 装上 git,然后用 git 安装:
$ cd /usr/local
$ git init
$ git remote add origin git://github.com/mxcl/
$ homebrew.git
$ git pull origin master
这么做的实际作用是把你的 /usr/local 目录变成了一个本地 git 仓库,只不过这个仓库只跟踪跟 Homebrew 相关的更新,并不影响任何其他软件的安装。
这样安装会在 /usr/local 下创建 Library 这个目录,然后在 /usr/local/bin 中加入 brew 这个 ruby 脚本。
$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
等待rvm安装好之后,配置环境变量,在当前用户目录下输入
sudo vim .bash_profile
在其中加入
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
source一下即可。
安装Ruby,并设置默认版本(当前最新的是1.9.3)
$ rvm install ruby --head
$ rvm use 1.9.3 --default
更换源
$ [sudo] gem update --system
$ [sudo] gem uninstall rubygems-update
$ [sudo] gem sources -r http://rubygems.org/
$ [sudo] gem sources -a http://ruby.taobao.org
$ gem install rails -V
至此,rails的应该已经可以使用了。
Mac下边就Textmate或者Macvim,我选的是后起之秀Sublime Text 2
详细的介绍和使用推荐Ruby-China的一篇帖子
一些常用的快捷键
首先是设置用户名和邮箱
$ git config --global user.name "Your Name" $ git config --global user.email youremail@example.com
$ git config --global color.diff auto
$ git config --global color.status auto
$ git config --global color.branch auto
$ git config --global color.interactive auto
$ git config --global color.ui auto
当然了也可以直接编辑~/.gitconfig
,查看配置$ git config --list
为了方便起见,做一些快捷设置(别名而已)
$ git config --global alias.co checkout
然后就是设置Git的默认编辑器,我用的是Sublime Text 2
$ git config --global core.editor "subl -w"
如果使用 textmate 就把subl -w换成mate -w,同理gvim对应 “gvim -f”,MacVim对应”mvim -f”,而gedit则对应”gedit -s”。
然后初始化一下下
$ git init
.gitignore
是忽略列表,Rails会自动生成一个的。
添加文件并且commit, -m 添加了一个消息,如果不写-m的话,会弹出编辑器让你添加的。
$ git add .
$ git commit -m "Initial commit"
使用 $ git status
可以查看当前版本的状态,$ git log
查看日志,$ git checkout -f
来恢复到上一个版本。
其实最好的教程还是官方的文档啦,简单全面,我在这里再稍微啰嗦几句。
首先要申请一个Github的账号,并且建立一个新的Repository,然后执行下面的命令
$ git remote add origin git@github.com:<username>/<Repositoryname>.git
$ git push origin master
这里其实是给Repository的地址起了一个别名而已,之后可以在项目文件夹下的.git/config
中修改和删除。
通过 -b 签出一个新的分支,不加-b的话就是切换分支,通过$ git branch
可以查看分支
$ git checkout -b new_branch
通过$ git merge <branch>
来合并分支,-d是删除,-D强制删除。
另一个比较常用的命令就是$ git push
了,当然了要想push到github上需要先生成密钥对
#ssh-keygen -t rsa -C "xxx@mail.com"。
把生成的密钥粘贴在Github的设置中。
补充一本书 Git Magic
安装并添加密钥对:
$ [sudo] gem install heroku
$ heroku keys:add
申请Heroku账号,并创建一个App
$ heroku create
Push过去就可以了
$ git push heroku master
折腾了半天,环境总算是弄好了,准备开发了。
这是一篇测试的文章。
这里是一段引用。
This is a code block.
class CleanRoom
def a_useful_method(x);x*2;end
end
Use the a_useful_method()
function.
唉?居然是这样的么,语法还是很不熟悉啊,关于代码应该还会有更好的解决方案吧,明天再弄吧。