标签归档: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 | 标签为 , , | 留下评论