亚洲喷奶水中文字幕电影,日本aⅴ高清一区二区三区,欧美亚洲日本国产,欧美日韩亚洲中文字幕

<legend id="flx4p"><abbr id="flx4p"><thead id="flx4p"></thead></abbr></legend>

<mark id="flx4p"><thead id="flx4p"></thead></mark>

      我要投稿 投訴建議

      有難度的Ruby專業(yè)面試試題

      時間:2022-08-04 15:11:21 面試試題 我要投稿
      • 相關(guān)推薦

      有難度的Ruby專業(yè)面試試題

        一、Ruby如何定義一個類

      有難度的Ruby專業(yè)面試試題

        類是對具有同樣屬性和同樣行為的對象的抽象,Ruby中類的聲明使用class關(guān)鍵字。定義類的語法如下,

        class ClassName

        def method_name(variables)

        #some code

        end

        end

        類的定義要在class…end之間,在上面的格式中,ClassName是類名,類名必須以大寫字母開始,也就是說類名要是個常量。

        看下面的例子:

        class Person

        def initialize(name, gender, age)

        @name = name

        @gender = gender

        @age = age

        end

        end

        若某個類已經(jīng)被定義過,此時又用相同的類名進(jìn)行類定義的話,就意味著對原有的類的定義進(jìn)行追加。

        class Test

        def meth1

        puts “This is meth1″

        end

        end

        class Test

        def meth2

        puts “This is meth2″

        end

        end

        在Test類中,原有meth1方法,我們又追加了meth2方法,這時候,對于Test類的對象,meth1和meth2同樣可用。

        二、如何安裝ruby on rails

        在安裝rails之前,要先安裝rubygems。rubygems是ruby的在線包管理工具,可以從rubyforge下載rubygems:

        http://rubyforge.org/projects/rubygems/

        下載好源代碼包,解壓縮,安裝:

        tar xzvf rubygems-0.9.0.tgz

        cd rubygems-0.9.0/

        ruby setup.rb

        然后就可以安裝rails了,在確認(rèn)服務(wù)器已經(jīng)連接互聯(lián)網(wǎng)的情況下執(zhí)行:

        gem install rails –y

        即通過gem從rubyforge網(wǎng)站下載rails所有依賴包安裝。

        安裝好rails以后,可以執(zhí)行:

        rails –v

        確認(rèn)一下rails的版本。

        三、如何將一個描述日期或日期/時間的字符串轉(zhuǎn)換為一個Date對象

        問題:如何將一個描述日期或日期/ 時間的字符串轉(zhuǎn)換為一個Date對象,其中,時間前面的字符串格式可能并不知道。

        參考答案:

        將日期字符串傳遞給Date.parse或DateTime.parse:

        require ‘date’

        Date.parse(’2/9/2007′).to_s

        # => "2007-02-09"

        DateTime.parse(’02-09-2007 12:30:44 AM’).to_s

        # => "2007-09-02T00:30:44Z"

        DateTime.parse(’02-09-2007 12:30:44 PM EST’).to_s

        # => "2007-09-02T12:30:44-0500"

        Date.parse(‘Wednesday, January 10, 2001′).to_s

        # => "2001-01-10"

        四、Ruby中的保護(hù)方法和私有方法與一般面向?qū)ο蟪绦蛟O(shè)計語言的一樣嗎

        Ruby中的保護(hù)方法和私有方法與一般面向?qū)ο蟪绦蛟O(shè)計語言的概念有所區(qū)別,保護(hù)方法的意思是方法只能方法只能被定義這個方法的類自己的對象和子類的對象訪問,私有方法只能被對象自己訪問。

        class Test

        def method1 #默認(rèn)為公有方法

        …

        end

        protected #保護(hù)方法

        def method2

        …

        end

        private #私有方法

        def method3

        end

        public

        def test_protected(arg) #arg是Test類的對象

        arg.method2 #正確,可以訪問同類其他對象的保護(hù)方法

        end

        def test_private(arg) #arg是Test類的對象

        arg.method3 #錯誤,不能訪問同類其他對象的私有方法

        end

        end

        obj1 = Test.new

        obj2 = Test.new

        obj1.test_protected(obj2)

        obj1.test_private(obj2)

        可以看到,和C++/Java相比,Ruby提供了更好的封裝性。