转自: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?
- def multiplier(n)
- lambda {|data| data.collect{|x| x*n } }
- end
- doubler = multiplier(2) # Get a lambda that knows how to double
- 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
- class AccessorUtil
- def initialize
- #1 you could write your code here
- end
- def method_missing(name,*args)
- #2 you could write your code here
- end
- end
- class StepUtil<AccessorUtil end
- test = StepUtil.new
- test.send "a1=",9333
- aaa= test.send "a1"
- puts aaa
8.How do hook method work, please give example of hook method
9.What is the result after execution.
- "ruby123"[/\d+/]
- "ruby123"[/([a-z]+)(\d+)/,1]
- "ruby123"[/([a-z]+)(\d+)/,2]
10. Point out the wrong code of Watir
- require 'watir'
- class WebPageUtil
- def initialize(ie)
- @dAttr=Hash.new
- @ie=ie
- @arr = ["text_field","checkbox","select_list","area","radio"]
- @filter = ["exampleid"]
- end
- def fill_form
- @dAttr.each do |name,value|
- if(!@filter.include?(name))
- @arr.each do |watir_method|
- begin
- el=@ie.send watir_method,:name,name
- if el.exist?
- case watir_method
- when "text_field"
- el.set value.to_s
- when "checkbox"
- if value.to_s =""
- el.clear
- else
- el.set
- end
- when "select_list"
- el.getAllContents
- el.set value.to_s
- when "area"
- el.append value.to_s
- when "radio"
- if value.to_s ==""
- el.clear
- else
- el.set
- end
- end
- end
- rescue
- end
- end
- end
- end
- end
- def method_missing(name,*args)
- key = name.to_s
- if name.to_s.include?("=")
- key = name.to_s.chop
- @dAttr[key]=args
- else
- if @dAttr.has_key?(name.to_s)
- return @dAttr[key].to_s
- else
- return nil
- end
- end
- end
- end
- ie = Watir::Browser.new
- ie.goto "http://www.google.com"
- dm= WebPageUtil.new(ie)
- dm.q="goole"
- dm.fill_form
11.Assumption you do the follow code refactor, what is your comment
- require 'win32ole'
- require 'lib/util/step_util'
- require 'watir'
- require 'lib/util/web_page_util'
- class ExcelUtil
- def getAbsolutePath filename
- fso = WIN32OLE.new('Scripting.FileSystemObject')
- return fso.GetAbsolutePathName(filename)
- end
- def read_step (filename,sheet)
- filename = getAbsolutePath(filename)
- puts filename
- xl = WIN32OLE.new('Excel.Application')
- book = xl.Workbooks.Open(filename)
- record = []
- begin
- sheet = book.Sheets(sheet)
- head = StepUtil.new
- index = 0
- sheet.UsedRange.Rows(1).Cells.each do |cells|
- index+=1
- head.send "column#{index}=",cells.value
- end
- sheet.UsedRange.Rows.each do |row|
- if(row.Cells(1).value!="xpath")
- step = StepUtil.new
- index_1 = 0
- row.Cells.each do |cell|
- index_1+=1
- attr = head.send "column#{index_1}"
- step.send "#{attr}=",cell.value
- end
- record<<step
- end
- end
- ensure
- book.Close
- xl.Quit
- end
- return record
- end
- def read_example (filename,sheet,ie)
- filename = getAbsolutePath(filename)
- puts filename
- xl = WIN32OLE.new('Excel.Application')
- book = xl.Workbooks.Open(filename)
- record = []
- begin
- sheet = book.Sheets(sheet)
- head = WebPageUtil.new(ie)
- index = 0
- sheet.UsedRange.Rows(1).Cells.each do |cells|
- index+=1
- head.send "column#{index}=",cells.value
- end
- sheet.UsedRange.Rows.each do |row|
- if(row.Cells(1).value!="exampleid")
- example = WebPageUtil.new(ie)
- index_1 = 0
- row.Cells.each do |cell|
- index_1+=1
- attr = head.send "column#{index_1}"
- example.send "#{attr}=",cell.value
- end
- record<<example
- end
- end
- ensure
- book.Close
- xl.Quit
- end
- return record
- end
- end
- example = ExcelUtil.new
- ie = Watir::Browser.new
- ie.goto "http://www.google.com"
- arr_ex = example.read_example("dos/example_group.xls","product_template",ie)
- arr_ex.each do |example|
- example.fill_form
- puts example
- puts "_________________________"
- End
12.There is an error in the following code, please correct
- require 'rubygems'
- require 'active_record'
- ActiveRecord::Base.establish_connection(
- :adapter => "oci",
- :username => "admin",
- :password => "123456",
- :host => "t31a.consys.prognet.com:1839/CTD09.real.com")
- class Account < ActiveRecord::Base
- set_table_name "ADMIN_TOOL_ACCOUNT"
- set_primary_key "id"
- end
- temp = Account.find_by_user_name("Admin")
- puts temp.id
- 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
- class A
- end
- class B<A
- def my_method
- puts "do something here"
- end
- end
- obj = B.new
- obj.send("my_method")