Rubyѧϰ±Ê¼ÇËÄ——Ä£¿é
#Ò»¡¢Ä£¿é¶¨Òå¼°ÒýÓã¬Ä£¿é¾ÍÊÇÒ»¶Î´úÂ룬ÀïÃæÓÐһЩ·½·¨·ÅÒ»Æð¡£
#¶¨ÒåÄ£¿éÓÃmodule...end ¡£Ä£¿éÓëÀà·Ç³£ÏàËÆ£¬µ«ÊÇ£º
#A) Ä£¿é²»¿ÉÒÔÓÐʵÀý¶ÔÏó£»
#B) Ä£¿é²»¿ÉÒÔÓÐ×ÓÀà¡£
include Math
puts sqrt(91);
module Me
def sqrt(a)
puts a*a;
return a*a;
end
PI=3.1415926
end
include Me
puts sqrt(10)
puts Math.sqrt(10);
puts Math::PI;
puts Me::PI;#±äÁ¿ÒýÓÃÓë·½·¨µ÷Óò»Í¬£¬ÓÃ˫ðºÅ£¬¶ø²»ÊǵãºÅ
#¶þ¡¢¿É±ä²ÎÊý
def params(*num)
"This is text sqrt. "
puts num.size
puts num[1]
puts num.inspect
end
params(4,2,3)
#Èý¡¢Ä£¿éµÄµÚÈý¸ö×÷Óã¬ÊµÏÖÀàËÆÓÚ¶àÖØ¼Ì³ÐµÄ¹¦ÄÜ£¬Ò²³ÆMix-in
#ÎÒÃÇÓÐÒ»¸öStudentÀ࣬ÓÐ×ÅPersonÀàµÄÊôÐԺͷ½·¨£¬»¹»á×öÊýѧÌâ——Ç󯽷½¸ù¡£ÒѾÓÐÁËMeÄ£¿é£¬Ö»ÒªMix-in ÔÚStudentÀàÀï¾Í¿ÉÒÔÁË¡£
module Me
def sqrt(num, rx=1, e=1e-10)
num*=1.0
(num - rx*rx).abs <e ? rx : sqrt(num, (num/rx + rx)/2, e)
end
end
class Person
def talk
puts "I'm talking."
end
end
class Student < Person
include Me
end
aStudent=Student.new
aStudent.talk # I'm talking.
puts aStudent.sqrt(20.7,3.3) # 4.54972526643248
#ͨ¹ý“ < ¸¸ÀàÃû ” £¬Ò»¸ö×ÓÀà¿ÉÒԵõ½¸¸ÀàµÄÊôÐԺͷ½·¨£»Í¨¹ý“ includeÄ£¿éÃû ” £¬Ò»¸ö×ÓÀà¿ÉÒԵõ½Ä³¸öÄ£¿éµÄ³£Á¿ºÍ·½·¨¡£À಻Äܱ» include ¡£
#ËÄ¡¢extend,»ìÐ͵ÄÁíÒ»ÖÖÇé¿ö
module Me
def sqrt(num, rx=1, e=1e-10)
num*=1.0
(num - rx*rx).abs <e ? rx : sqrt(num, (num/rx + rx)/2, e)
end
end
class Student
end
aStudent=Student.new
aStudent.extend(Me)
puts aStudent.sqrt(93.1, 25) # 9.64883412646315
#include ·½·¨ÎªÒ»¸öÀàµÄËùÓжÔÏó°üº¬Ä³¸öÄ£¿é£» extend ·½·¨ÎªÒ»¸öÀàµÄij¸ö¶ÔÏó°üº¬Ä³¸öÄ£¿é¡£
#Îå¡¢requireºÍload
#require£¬loadÓÃÓÚ°üº¬Îļþ£»include£¬extendÔòÓÃÓÚ°üº¬Ä£¿é¡£
#require¼ÓÔØÎļþÒ»´Î£¬load¼ÓÔØÎļþ¶à´Î¡£
#require¼ÓÔØÎļþʱ¿ÉÒÔ²»¼Óºó׺Ãû£¬load¼ÓÔØÎļþʱ±ØÐë¼Óºó׺Ãû¡£
#requireÒ»°ãÇé¿öÏÂÓÃÓÚ¼ÓÔØ¿âÎļþ£¬¶øloadÓÃ
Ïà¹ØÎĵµ£º
class Tree
def initialize
puts "Make a normal tree"
end
def decorate
puts "Make sure the tree won\'t fall"
end
end
class RedBalls < Tree
def initialize(tree)
@parent = tree;
end
def decorate
@parent.decorate
puts "Put on some red balls"
end
end
......
ÔÚÂÛ̳Àï¿´ÁËdaquan198163ËùдµÄ¡¶³õѧRORµÄÒÉ»ó¡·ÕâÆªÎÄÕ£¬robbinÔڻظ´ÖиøÁËÏêϸµÄ½â´ð£¬¿ÉÊÇ¿´Íê½â´ðÖ®ºó£¬ÎÒÒ²²úÉúÁËһЩÒÉ»ó£¬ÔÚ´ËÏëÎÊÎÊ¡£
ÔÚrobbinËù¸ø³öµÄ½â´ðµ±ÖУ¬¸ü¶àµÄÊDzûÊörailsµÄºÃ´¦£¬È»¶ø´ó¼Ò¶¼ÖªµÀ£¬railsÖ»Ê ......
rubyÖÐ×Ô´øÊµÏÖ¹Û²ìÕßģʽµÄÀàobserver¡£¿ÉÒÔÀûÓÃËüÀ´ÊµÏÖ¹Û²ìÕßģʽ¡£
´úÂëÀý×Ó£º
# -*- coding: GB2312 -*-
require 'observer'
# ¹Û²ìÕßģʽ(ruby)µÄʹÓÃÀý×Ó
# ±»¹Û²ìÕßP
class PObservable
include Observable
end
# ¹Û²ìÕßA
class AObserver
# update·½·¨ÃûÊDZØÐëµÄÒªÓеÄ
def update(arg)
puts "AO ......
ΪÁËÑ¡ÔñÒ»¸öºÏÊʵĽű¾ÓïÑÔѧϰ£¬½ñÌì²éÁ˲»ÉÙÓйØPerl£¬Python£¬Ruby£¬JavascriptµÄ¶«Î÷£¬¿ÉÊÇ·¢ÏÖ¸÷´óÕóÓªµÄÈ˶¼ÔÚ´µÅõ×Ô¼ºÏ²»¶µÄÓïÑÔ£¬²»¹ý×îûÓÐÕùÒéµÄÓ¦¸ÃÊÇJavascriptÏֽ׶λ¹²»ÊʺÏÓÃÀ´×ö¶ÀÁ¢¿ª·¢£¬ËüµÄÌìÏ»¹ÊÇÔÚwebÓ¦ÓÃÉÏ¡£ ÎÒÖ÷ÒªÊÇÏë×öÊý¾ÝÍÚ¾òËã·¨µÄÑо¿£¬Ó¦¸Ã»á´¦Àí´óÁ¿µÄÎı¾¡£Ìáµ½Îı¾´¦Àí£¬ÏàÐŴ󲿷ÖÈË ......
http://www.gayathri-frenzy.com/technology/ruby-on-rails
I kept thinking for a while on what do I have next in the store
Here we go “Ruby on Rails”
Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby Programming language.Ruby is a ......