標籤歸檔:rubyonrails

mysql不能儲存生僻漢字?試試utf8mb4字符集

今天發現存進mysql的數據部分丟失了。發現丟失的都是一些生僻的漢字。我這個mysql使用的是utf8字符集,之前一直以為不會發生這種問題,今天才看到了。而且這個問題很早就存在了。這是mysql的一個bug,它的UTF-8字符集只能存儲3字節字符,而有部分生僻的漢字是4個字節的,存入時就會出錯:

ActiveRecord::StatementInvalid: Mysql::Error: Incorrect string value: ‘\xF0\x90\x8D\x83\xF0\x90…’ for column ‘content’ at row 1

上面的官方bug頁面說要到mysql6.0才會解決。其實這個問題在mysql 5.5就解決了。5.5或以上的mysql都可以通過使用utf-16或utf8mb4等字符集解決這個問題。

不過使用ruby on rails的話可能需要一些額外的設置

發表在 信息處理 | 標籤為 , , , , , | 留下評論

太噁心了– rubygems安裝gem需要使用代理服務器了

換了一台電腦,想用rubygems重新安裝rails,但一直遇到各種如問題:

sudo gem install -v=2.3.9 rails
Password:
ERROR:  Could not find a valid gem 'rails' (= 2.3.9) in any repository
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
    too many connection resets (http://production.s3.rubygems.org/latest_specs.4.8.gz)

ERROR: While executing gem ... (Gem::DependencyError)
  Unable to resolve dependencies: rails requires activesupport (= 3.0.5), actionpack (= 3.0.5), activerecord (= 3.0.5), activeresource (= 3.0.5), actionmailer (= 3.0.5), railties (= 3.0.5), bundler (~> 1.0)

google了半天(期間google中短NNN次),才明天了現在使用rubygems 安裝gem也得開代理了。。。

要使用加了代理的命令:

sudo gem install rails -p http://127.0.0.1:8118

現在rubygems好像只支持http代理。 如果你使用的是socks代理,可以通過Privoxy(支持linux和mac)這個軟件來搞一個http代理 通道, 詳細參看這個教程

發表在 Ruby on Rails | 標籤為 , , , | 一條評論

使用Unicorn替代Mongrel作為Ruby on Rails的服務器

先前的開發的Ruby on Rails網站使用的服務程序是Mongrel + Nginx, 現在用了Rails 3, 發現Mongrel對它有兼容問題, 所以要換一個。 雖然現在Phusion Passenger大行其道, 但使用前要重新編譯Nginx。 為了避免重新安裝nginx,我找到mongrel的替代品Unicorn。 沒想到unicorn的設置非常方便。 我記錄在這裡供大家參考:

安裝unicorn:

sudo gem install unicorn

創建網站配置文件(myproject是項目名稱):
繼續閱讀

發表在 Ruby on Rails, 站長文檔 | 標籤為 , , , , | 一條評論

Extreme Programming and Test-Driven Development

Extreme Programming
Extreme Programming (or XP) is a software engineering methodology, the most prominent of several agile software development methodologies.

Extreme Programming Explained describes Extreme Programming as being:
    * An attempt to reconcile humanity and productivity
    * A mechanism for social change
    * A path to improvement
    * A style of development
    * A software development discipline


Test-Driven Development
The goal of TDD is to write clean code that works.

What is Test-Driven Development?
Test-Driven Development (TDD) is a software development technique that involves repeatedly first writing a test case and then implementing only the code necessary to pass the test. Test-driven development is a method of designing software, not merely a method of testing.

In addition to normal “did it pass?” testing, you can go the opposite route. By testing your application where the weak points are, you can fix it before it ever becomes an issue.

Test-Driven Development Cycle
1. Add a test
In order to write a test, the developer must understand the specification and the requirements of the feature clearly.
2. Run all tests and see the new one fail
testing the tests
3. Write some code
It is important that the code written is only designed to pass the test; no further (and therefore untested) functionality should be predicted and ‘allowed for’ at any stage.

KISS
Keep It Simple, Stupid.
Everything should be made as simple as possible, but no simpler.
                    –Albert Einstein

You Ain’t Gonna Need It
‘You Ain’t Gonna Need It’(YAGNI), is a reminder for programmers that one should never add functionality until it is necessary.

4. Run the automated tests and see them succeed
5. Refactor code

Refactoring
A code refactoring is any change to a computer program which improves its readability or simplifies its structure without changing its results.
List of refactorings

    * Encapsulate Field(e.g. providing methods that can be used to read/write to/from the field rather than accessing the field directly.)
    * Extract Method (to turn part of a larger method into a new method. By breaking down code in smaller pieces, it is more easily understandable. This is also applicable to functions)
    * Generalize Type (to making more general or more abstract some subset of the traits of a specific type. An example of generalizing a type would be moving a method from a child to a parent class for common use by all the parent class’ children, not just the original child.)
    * Pull Up (moving a method from a Subclass into a Superclass. )
    * Push Down (moving a method from a SuperClass into a SubClass.)
    * Rename Method (changing the name of a method into a new one that better reveals its purpose).

The cycle is then repeated, starting with another new test to push forward the functionality.

"Test-Driven Development Mantra" is known as red/green/refactor where red means fail and green is pass.

Benefits
By focusing on the test cases first, one must imagine how the functionality will be used by clients (in this case, the test cases). Therefore, the programmer is only concerned with the interface and not the implementation.
It allows a programmer to focus on the task at hand as the first goal is to make the test pass.
Ensuring that all written code is covered by a test.

Limitations
A test-driven development is only as good as its tests.

Reference
http://en.wikipedia.org/wiki/Test-driven_development
http://en.wikipedia.org/wiki/You_Ain%27t_Gonna_Need_It
http://en.wikipedia.org/wiki/Refactoring

Related Resources
Test::Unit – Ruby Unit Testing Framework
A Guide to Testing the Rails
Test Driven Design for Ruby and Rails
Ruby, Rails, Test::Rails Cheat Sheet

發表在 Ruby on Rails | 標籤為 , , | 留下評論