Showing posts with label Console. Show all posts
Showing posts with label Console. Show all posts

Sunday, October 05, 2008

Firebug command line api 和 console api 中比较常用到的几个方法

1. $(id) 与 prototype 用法类似,只能传字符串的Element Id。
2. $$(selector) 与 prototype 用法类似,传一个CSS Selector字符串,另专门还有一个$x("xPath")方法接受xpath方式查询DOM节点。
3. dir(object) 遍历object所有属性,在console中打印出来。
4. clear() 清空console。
5. debug(fn) 在对应的fn方法第一行加一个断点,当此方法调用时就会进入此方法第一行,可进行断点调试此方法。对应的有undebug(fn)取消断点。

6. console.log(object[, object, ...]),console是Firebug在Firefox中添加的一个全局变量名。其log()方法会将对象打印到firebug控制台中,支持类似printf()的功能。
7. 同console.log()类似的还有console.debug(),console.info(),console.warn(),console.error()方法。
8. console.assert(expression[, object, ...])断言功能,用来检查表达式是否正确执行。

Reference: http://getfirebug.com/commandline.html
http://getfirebug.com/console.html

Friday, November 16, 2007

R4R and PR2 environment top-level method 学习笔记

R4R
Defining a top-level method
Suppose you define a method at the top level:


def talk
puts "Hello"
end

Who, or what, does the method belong to? It’s not inside a class or module definition block, so it doesn’t appear to be an instance method of a class or module. It’s not attached to any particular object (as in def obj.talk), so it’s not a singleton method. What is it?
By special decree (this is just the way it works!), top-level methods are private instance methods of the Kernel module.
That decree tells you a lot.
Because top-level methods are private, you can’t call them with an explicit receiver; you can only call them by using the implied receiver, self. That means self must be an object on whose method search path the given top-level method lies.

The default object (self) and scope
But every object’s search path includes the Kernel module, because the class Object mixes in Kernel, and every object’s class has Object as an ancestor. That means you can always call any top-level method, wherever you are in your program.
It also means you can never use an explicit receiver on a top-level method.
To illustrate this, let’s extend the talk example. Here it is again, with some code that exercises it:

def talk
puts "Hello"
end
puts "Trying 'talk' with no receiver..."
talk
puts "Trying 'talk' with an explicit receiver..."
obj = Object.new
obj.talk

The first call to talk succeeds; the second fails, because you’re trying to call a private method with an explicit receiver.
The rules concerning definition and use of top-level methods brings us all the way back to some of the bareword methods we’ve been using since as early as chapter1(R4R book). You’re now in a position to understand exactly how those methods work.

Programming Ruby 2nd
Top-Level Execution Environment
Many times in this book we’ve claimed that everything in Ruby is an object. However, we’ve used one thing time and time again that appears to contradict this—the top-level Ruby execution environment.
puts "Hello, World"
Not an object in sight. We may as well be writing some variant of Fortran or BASIC.
But dig deeper, and you’ll come across objects and classes lurking in even the simplest code.
We know that the literal "Hello, World" generates a Ruby String, so that’s one object. We also know that the bare method call to puts is effectively the same as self.puts. But what is self?
self.class ! Object
At the top level, we’re executing code in the context of some predefined object. When we definemethods, we’re actually creating (private) instancemethods for class Object.
This is fairly subtle; as they are in class Object, these methods are available everywhere.
And because we’re in the context of Object, we can use all of Object’s methods (including those mixed-in from Kernel) in function form. This explains why we
can call Kernel methods such as puts at the top level (and indeed throughout Ruby): these methods are part of every object. Top-level instance variables also belong to this top-level object.

命令行中定义的其实是定义在Kernel中的私有方法,并被Mix-in到Object类中成为其对象私有实例方法。因为class Module (< Object)和class Class (< Module)这3者的继承关系,所以控制台里定义的方法在module和class实例对象中可以调用。

Monday, September 10, 2007

no such file to load -- readline (LoadError)

$shell>ruby script/console
Loading development environment.
/usr/local/lib/ruby/1.8/irb/completion.rb:10:in `require': no such file to load -- readline (LoadError)
from /usr/local/lib/ruby/1.8/irb/completion.rb:10
from /usr/local/lib/ruby/1.8/irb/init.rb:252:in `load_modules'
from /usr/local/lib/ruby/1.8/irb/init.rb:250:in `load_modules'
from /usr/local/lib/ruby/1.8/irb/init.rb:21:in `setup'
from /usr/local/lib/ruby/1.8/irb.rb:54:in `start'
from /usr/local/bin/irb:13

ubutu7.04 上解决方法见:
https://rubyforge.org/forum/forum.php?thread_id=14854&forum_id=9502
centos5.0 上解决方法如下:
yum install readline-devel
$shell> cd /path/to/ruby_source/ext/readline
$shell> ruby extconf.rb
$shell> make
$shell> sudo make install

Sunday, August 26, 2007

Rails Routes 详解

$>cd RubyOnRails/app/path
$>cd script/console
>>rs = ActionController::Routing::Routes
>>puts rs.routes
ANY /:controller/service.wsdl/ {:action=>"wsdl"}
ANY /:controller/:action/:id.:format/ {}
ANY /:controller/:action/:id/ {}
=>nil

修改config/routes.rb文件,添加一行
map.connect 'modules/controller/:action/*other', :controller => 'modules/controller'
回到console
>>rs.reload
>>puts rs.routes
ANY /:controller/service.wsdl/ {:action=>"wsdl"}
ANY /:controller/:action/:id.:format/ {}
ANY /:controller/:action/:id/ {}
ANY /modules/controller/:action/:other/ {:controller=>"modules/controller"}
=>nil
>>rs.recognize_path '/modules/controller/test/2?x=1'
=>{:action=>"test", :other=>["2?x=1"], :controller=>"modules/controller"}
>>rs.recognize_path '/test/action/1'
=>{:action=>"action", :id=>"1", :controller=>"test"}
>> rs.add_route('/x/y/z', {:controller => 'test', :action => 'index', :id => 0})=> #>>puts rs.routes
ANY /:controller/service.wsdl/ {:action=>"wsdl"}
ANY /:controller/:action/:id.:format/ {}
ANY /:controller/:action/:id/ {}
ANY /modules/controller/:action/:other/ {:controller=>"modules/controller"}
ANY /x/y/z/ {:action=>"index", :id=>0, :controller=>"test"}
=> nil
>> rs.draw do |map|
?> map.connect '/blog/:id', :controller => 'blog', :action => 'list'
>> end
=> [ActionController::Base, ActionView::Base]
>> puts rs.routes
ANY /blog/:id/ {:action=>"list", :controller=>"blog"}
=> nil
>> rs.recognize_path '/blog/1'
=> {:action=>"list", :id=>"1", :controller=>"blog"}
这里发现原来的routes全部不见了,只有这个新建的routes了,因为draw方法内部实现为:


def draw
clear!
yield Mapper.new(self)
named_routes.install
end

draw方法块里的map对象里method_missing方法就是用来实现具名路由named routes的。先看下源码如下:

def method_missing(route_name, *args, &proc)
super unless args.length >= 1 && proc.nil?
@set.add_named_route(route_name, *args)
end

上面对实例变量@set就是前面的rs对象,那么就看一下rs.add_named_route方法吧
>>rs.clear!
>>rs.add_named_route('index', 'user/index', :controller => 'user', :action => 'index')
puts rs.named_routes
#
=> nil
>> puts rs.routes
ANY /user/index/ {:action=>"index", :controller=>"user"}
=> nil
>> puts rs.named_routes.routes
indexANY /user/index/ {:action=>"index", :controller=>"user"}
=> nil
定义了具名路后就可以在controller/view中像url_for一样调用,只要在route name后面加上_url就可以,url_for在console里不能调试,可以用rs.generate(request)方法测试。
# In addition to providing url_for, named routes are also accessible after including UrlWriter.
url_for(:controller => 'signup', action => 'index', :token => token)
index_url(:id => 1)
#=> http://localhost/user/index?id=1
>> rs.generate(:controller => 'test', :action => 'index', :id => 3)
=> "/test/index/3"
另外在map这个对象里有个方法root可以给根目录定义路径,用法如下:

>> rs.draw do |map|
?> map.root :controller => 'test', :action => 'list'
>> end
ArgumentError: Illegal route: the :controller must be specified!
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:965:in `build'
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1172:in `add_route'
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1178:in `add_named_route'
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:997:in `root_without_deprecation'
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/deprecation.rb:94:in `root'
from (irb):60
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1139:in `draw'
from (irb):59


# Added deprecation notice for anyone who already added a named route called "root".
# It'll be used as a shortcut for map.connect '' in Rails 2.0.
# def root(*args, &proc)
# super unless args.length >= 1 && proc.nil?
# @set.add_named_route("root", *args)
# end
# deprecate :root => "(as the the label for a named route) will become a shortcut for map.connect '', so find another name"

Rails2.0才会引进此方法,所以Rails建议用户不要使用此具名路由,如下:
>> rs.draw do |map|
?> map.root '', :controller => 'test', :action => 'list'
>> end
=> [ActionController::Base, ActionView::Base]
>> puts rs.routes
ANY / {:action=>"list", :controller=>"test"}
=> nil
>> puts rs.named_routes.routes
rootANY / {:action=>"list", :controller=>"test"}
=> nil
这个写法其实就是做了个具名路由,而不是调用map.root方法,调用的是method_missing方法。

Wednesday, May 30, 2007

rails script/console problem on ubuntu

shell> ruby script/console
Loading development environment.

/usr/local/lib/ruby/1.8/irb/completion.rb:10:in `require':

no such file to load -- readline (LoadError)

from /usr/local/lib/ruby/1.8/irb/completion.rb:10

from /usr/local/lib/ruby/1.8/irb/init.rb:252:in `load_modules'

from /usr/local/lib/ruby/1.8/irb/init.rb:250:in `load_modules'

from /usr/local/lib/ruby/1.8/irb/init.rb:21:in `setup'

from /usr/local/lib/ruby/1.8/irb.rb:54:in `start'

from /usr/local/bin/irb:13

Check them out:
http://ubuntuforums.org/showthread.php?t=169891
http://blog.nanorails.com/articles/2006/03/06/installing-readline-on-kubuntu

1.) Download ruby source.
Download Here
2.) Extract the tar file.
tar -xvf ruby-1.8.5-p12.tar.gz
3.) Change to that directory
cd ruby-1.8.5-p12.tar.gz
4.) Type these commands.
./configure
make
sudo make install
5.) Install readline and ncurses.
apt-get install libncurses5-dev libreadline5-dev
6.) go back to the folder ruby source folder.
ruby-1.8.5-p12.tar.gz
7.) Type these commands
cd ext
cd readline
ruby extconf.rb
make
sudo make install

Thats it. The console bug no longer appears.