Rails 笔试题

130 篇文章 0 订阅

转自:http://hlee.iteye.com/blog/571352

1.What is the notation used for denoting class variables in Ruby? 


2.How is class methods defined in Ruby?  

3. Whats the difference between symbol and string?  

4.What  it's print in screen?  

Ruby代码   收藏代码
  1. def multiplier(n)   
  2.   lambda {|data| data.collect{|x| x*n } }  
  3. end  
  4. doubler = multiplier(2)     # Get a lambda that knows how to double  
  5. puts doubler.call([1,2,3])    

5.What is the use of load and require in Ruby?  
6.How do Dynamic define a Methods  and call a methods dynamically  
7.Use  method_missing()  implement dynamic attribute 
Ruby代码   收藏代码
  1.   class AccessorUtil  
  2.   def initialize  
  3.       #1 you could write your code here  
  4.   end  
  5.   
  6.   def method_missing(name,*args)  
  7.       #2 you could write your code here  
  8.   end  
  9. end   
  10. class StepUtil<AccessorUtil  end  
  11. test = StepUtil.new  
  12. test.send "a1=",9333  
  13. aaa= test.send "a1"  
  14. puts aaa  

8.How do hook method work, please give example of hook method  

9.What is the result after execution.  
Ruby代码   收藏代码
  1.    "ruby123"[/\d+/]   
  2.     "ruby123"[/([a-z]+)(\d+)/,1]    
  3. "ruby123"[/([a-z]+)(\d+)/,2]  


10. Point out the wrong code of Watir  
Ruby代码   收藏代码
  1. require 'watir'  
  2.   
  3. class WebPageUtil  
  4.   def initialize(ie)  
  5.     @dAttr=Hash.new  
  6.     @ie=ie  
  7.     @arr = ["text_field","checkbox","select_list","area","radio"]  
  8.     @filter = ["exampleid"]  
  9.   end  
  10.   
  11.   def fill_form  
  12.     @dAttr.each do |name,value|  
  13.       if(!@filter.include?(name))  
  14.         @arr.each do |watir_method|  
  15.           begin  
  16.             el=@ie.send watir_method,:name,name  
  17.             if el.exist?  
  18.               case watir_method  
  19.               when "text_field"  
  20.                 el.set value.to_s  
  21.               when "checkbox"  
  22.                 if value.to_s =""  
  23.                   el.clear  
  24.                 else  
  25.                   el.set  
  26.                 end  
  27.               when "select_list"  
  28.                 el.getAllContents  
  29.                 el.set value.to_s  
  30.               when "area"  
  31.                 el.append value.to_s  
  32.               when "radio"  
  33.                 if value.to_s ==""  
  34.                   el.clear  
  35.                 else  
  36.                   el.set  
  37.                 end  
  38.               end  
  39.             end  
  40.           rescue  
  41.           end  
  42.         end  
  43.       end  
  44.     end  
  45.   
  46.   end  
  47.   
  48.   def method_missing(name,*args)  
  49.     key = name.to_s  
  50.     if name.to_s.include?("=")  
  51.       key = name.to_s.chop  
  52.       @dAttr[key]=args  
  53.     else  
  54.       if @dAttr.has_key?(name.to_s)  
  55.         return @dAttr[key].to_s  
  56.       else  
  57.         return nil  
  58.       end  
  59.     end  
  60.   end  
  61. end  
  62.   
  63. ie = Watir::Browser.new  
  64. ie.goto "http://www.google.com"  
  65. dm= WebPageUtil.new(ie)  
  66. dm.q="goole"  
  67. dm.fill_form  

11.Assumption you do the follow code refactor, what is your comment 
Ruby代码   收藏代码
  1. require 'win32ole'  
  2. require 'lib/util/step_util'  
  3. require 'watir'  
  4. require 'lib/util/web_page_util'  
  5.   
  6. class ExcelUtil  
  7.   def getAbsolutePath filename  
  8.     fso = WIN32OLE.new('Scripting.FileSystemObject')  
  9.     return fso.GetAbsolutePathName(filename)  
  10.   end  
  11.   def read_step (filename,sheet)  
  12.       filename = getAbsolutePath(filename)  
  13.       puts filename  
  14.       xl = WIN32OLE.new('Excel.Application')  
  15.       book = xl.Workbooks.Open(filename)  
  16.       record = []  
  17.       begin  
  18.         sheet = book.Sheets(sheet)  
  19.         head = StepUtil.new  
  20.         index = 0  
  21.         sheet.UsedRange.Rows(1).Cells.each do |cells|  
  22.            index+=1  
  23.            head.send "column#{index}=",cells.value  
  24.         end  
  25.           sheet.UsedRange.Rows.each do |row|  
  26.            if(row.Cells(1).value!="xpath")  
  27.               step = StepUtil.new  
  28.               index_1 = 0  
  29.              row.Cells.each do |cell|  
  30.                index_1+=1  
  31.                attr = head.send "column#{index_1}"  
  32.                  
  33.                step.send "#{attr}=",cell.value  
  34.              end  
  35.                
  36.             record<<step    
  37.            end  
  38.           end  
  39.       ensure  
  40.         book.Close  
  41.         xl.Quit  
  42.       end  
  43.       return record  
  44.     end  
  45.       
  46.   def read_example (filename,sheet,ie)  
  47.         filename = getAbsolutePath(filename)  
  48.         puts filename  
  49.         xl = WIN32OLE.new('Excel.Application')  
  50.         book = xl.Workbooks.Open(filename)  
  51.         record = []  
  52.         begin  
  53.           sheet = book.Sheets(sheet)  
  54.           head = WebPageUtil.new(ie)  
  55.           index = 0  
  56.           sheet.UsedRange.Rows(1).Cells.each do |cells|  
  57.              index+=1  
  58.              head.send "column#{index}=",cells.value  
  59.           end  
  60.             sheet.UsedRange.Rows.each do |row|  
  61.              if(row.Cells(1).value!="exampleid")  
  62.                 example = WebPageUtil.new(ie)  
  63.                 index_1 = 0  
  64.                row.Cells.each do |cell|  
  65.                  index_1+=1  
  66.                  attr = head.send "column#{index_1}"  
  67.                    
  68.                  example.send "#{attr}=",cell.value  
  69.                end  
  70.                  
  71.               record<<example   
  72.              end  
  73.             end  
  74.         ensure  
  75.           book.Close  
  76.           xl.Quit  
  77.         end  
  78.         return record  
  79.       end  
  80. end  
  81.   
  82.   
  83. example = ExcelUtil.new  
  84. ie = Watir::Browser.new  
  85. ie.goto "http://www.google.com"  
  86. arr_ex = example.read_example("dos/example_group.xls","product_template",ie)  
  87.   arr_ex.each do |example|  
  88.     example.fill_form  
  89.     puts example         
  90.     puts "_________________________"  
  91.   End  

12.There is an error in the following code, please correct
Ruby代码   收藏代码
  1. require 'rubygems'  
  2. require 'active_record'    
  3.   
  4. ActiveRecord::Base.establish_connection(  
  5.     :adapter => "oci",   
  6.     :username => "admin",   
  7.     :password => "123456",   
  8.     :host => "t31a.consys.prognet.com:1839/CTD09.real.com")  
  9.   
  10.       
  11. class Account < ActiveRecord::Base  
  12.   set_table_name "ADMIN_TOOL_ACCOUNT"  
  13.   set_primary_key "id"  
  14.     
  15. end  
  16.   
  17. temp = Account.find_by_user_name("Admin")  
  18. puts temp.id  
  19. puts temp.USER_NAME  


13.Explain how Spec::Mocks work,and give an example  
14. In ruby world, who hold object instance variable 

A. Kernel  B. Class  C. Object  D. Module 

15.Write down "my_method" Method lookup chain
Ruby代码   收藏代码
  1. class  A  
  2. end  
  3.   
  4. class  B<A  
  5.    def my_method  
  6.           puts  "do something here"  
  7.        end  
  8. end  
  9.   
  10.  obj = B.new  
  11.   obj.send("my_method")  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值