Rubyʵ¼ù—HelloWorld
¿ª·¢»·¾³
OS£ºWindowsXP
Ruby: Ruby1.9.1
Rails: Rails2.3.5
IDE: RubyMine2.0.1
1¡¢´´½¨Rails¹¤³Ì
2¡¢ÐÞ¸Ä /config/database.yml
×Ô¶¯´´½¨µÄ¹¤³ÌÖÐĬÈÏÊý¾Ý¿âÁ¬½ÓµÄÊÇsqlite£¬Èç¹ûûÓа²×°´ËÊý¾Ý¿â£¬ÐèÒªÐ޸ĸÃÅäÖ㨱¾ÀýÖÐʹÓõÄÊÇmysql£©
# Mysql Version 5.1.46
development:
adapter: mysql
database: test
username: root
password: root
host: localhost
3¡¢´´½¨Controller
ÔÚapp/controllerÖд´½¨say_controller.rb
´´½¨Íê³Éºó£¬ÔÚ¿ØÖÆÌ¨ÐÅÏ¢Öн«ÏÔʾ´Ë¹ý³Ì´´½¨µÄһϵÁÐÎļþ
C:\Ruby19\bin\ruby.exe -e STDOUT.sync=true;STDERR.sync=true;load($0=ARGV.shift) E:/Ruby/HelloWorld/script/generate controller -s say
exists app/controllers/
exists app/helpers/
create app/views/say
exists test/functional/
create test/unit/helpers/
create app/controllers/say_controller.rb
create test/functional/say_controller_test.rb
create app/helpers/say_helper.rb
create test/unit/helpers/say_helper_test.rb
Process finished with exit code 0
4¡¢ÐÞ¸Äsay_controller.rb
½«ÄÚÈÝÐÞ¸ÄÈçÏ£º
class SayController < ApplicationController
def hello
end
end
5¡¢´´½¨hello.rhtml.erb
ÔÚapp/views/say Ŀ¼Ï´´½¨hello.rhtml.erb
ÐÞ¸ÄÆäÖеÄÄÚÈÝÈçÏ£º
<html>
<head>
<title>Hello, Rails!</title>
</head>
<body>
<h1>Hello from Rails!</h1>
</body>
</html>
6¡¢ÐÞ¸Äroutes.rb
ÐÞ¸Äconfig/routes.rb£¬ÉèÖÃеÄÓ³É乿Ôò
#·ÓÉÉèÖã¬/say/HelloÊǵØÖ·ÉèÖã»controllerÊǶÔÓ¦µÄcontrollersĿ¼ÏµÄÀ࣬actionÖ¸controllerÖж¨ÒåµÄ·½·¨£¬Çø±ð´óСд
map.connect '/say/Hello',:controller=>"say",:action=>"hello"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
Æô¶¯·þÎñRails·þÎñÆ
Ïà¹ØÎĵµ£º
ÁùÖÖÓÃrubyµ÷ÓÃÖ´ÐÐshellÃüÁîµÄ·½·¨
Åöµ½ÐèÒªµ÷ÓòÙ×÷ϵͳshellÃüÁîµÄʱºò£¬RubyΪÎÒÃÇÌṩÁËÁùÖÖÍê³ÉÈÎÎñµÄ·½·¨£º
1.Exec·½·¨:
Kernel#exec·½·¨Í¨¹ýµ÷ÓÃÖ¸¶¨µÄÃüÁîÈ¡´úµ±Ç°½ø³Ì£º
Àý×Ó£º
$ irb
>> exec 'echo " ......
ת Adding Sound to Your Ruby Apps
Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Per ......
¡¡×î½ü£¬ÎÒÐèÒªÒ»ÖÖ¿ìËÙ¿ª·¢ÓïÑÔ£¬Ä¿Ç°ÖËÊÖ¿ÉÈȵĿìËÙ¿ª·¢ÓïÑÔ¾ÍÊÇPythonºÍRubyÁË¡£RubyºÍPython¶¼ÊǷdz£ÓÅÐãµÄ½Å±¾ÓïÑÔ£¬ËäÈ»ËûÃǵÄÉè¼ÆÕÜѧ¼¸ºõÍêÈ«²»Í¬£¬µ«ËûÃÇЧÂʲ¶à£¬Ó¦ÓÃÁìÓòÒ²²î¾à²»´ó£¬ËƺõÌìÉú¾ÍÊǾºÕù¶ÔÊÖ¡£µ½µ×Ñ¡ÄǸöÄØ?ÎÒ×öÁËһЩ¼òµ¥µÄ±È½Ï¡£
¡¡¡¡ÎÒÃDZȽ ......
selfÉÏÏÂÎÄ
RubyµÄselfÓкÍJavaµÄthisÏàËÆÖ®´¦£¬µ«ÓÖ´ó²»Ïàͬ¡£JavaµÄ·½·¨¶¼ÊÇÔÚʵÀý·½·¨ÖÐÒýÓã¬ËùÒÔthisÒ»°ã¶¼ÊÇÖ¸Ïòµ±Ç°¶ÔÏóµÄ¡£¶øRubyµÄ´úÂëÖðÐÐÖ´ÐУ¬ËùÒÔÔÚ²»Í¬µÄÉÏÏÂÎÄ(context)self¾ÍÓÐÁ˲»Í¬µÄº¬Ò壬ÏÈÀ´¿´¿´³£¼ûµÄcontext self¶¼´ú±íÄÄЩ
1
2
3
4
5
6
7
8
9
10
11
12
13
......