`
xu_wccq
  • 浏览: 128355 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

(ruby)String Extensions(字符串、首字母大写,复数单数转换)

    博客分类:
  • ruby
阅读更多
字符串截取,
英文单词单数复数转换(Agile Web Development with Rails [Chapter 15||||||||||||||Active Support[P251])
ruby 代码
  1. string = "Now is the time"
  2. puts string.at(2) #=> "w"
  3. puts string.from(8) #=> "he time"
  4. puts string.to(8) #=> "Now is th"
  5. puts string.first #=> "N"
  6. puts string.first(3) #=> "Now"
  7. puts string.last #=> "e"
  8. puts string.last(4) #=> "time"
  9. puts string.starts_with?("No" ) #=> true
  10. puts string.ends_with?("ME" ) #=> false
  11. count = Hash.new(0)
  12. string.each_char {|ch| count[ch] += 1}
  13. puts count.inspect #=> {" "=>3, "w"=>1, "m"=>1, "N"=>1, "o"=>1,
  14. "e" =>2, "h" =>1, "s" =>1, "t" =>2, "i" =>2}
  15. Active Support adds methods to all strings to support the way Rails itself
  16. converts names from singular to plural, lowercase to mixed case, and so on. A
  17. few of these might be useful in the average application.
  18. "cat".pluralize
  19. puts #=> cats
  20. "cats".pluralize
  21. puts #=> cats
  22. "erratum".pluralize
  23. puts #=> errata
  24. "cats".singularize
  25. puts #=> cat
  26. "errata".singularize
  27. puts #=> erratum
  28. "first_name".humanize
  29. puts #=> "First name"
  30. "now is the time".titleize
  31. puts #=> "Now Is The Time"

ruby 代码
 
  1. =begin  
  2. # delete hash key if it not in `arr_need` and it exists in `arr_del` element  
  3. hash={"previous"=>nil"operateDescription"=>nil"channel_status"=>["E""R""P""Q"], "next"=>nil"lastOperator"=>nil}  
  4. arr_need=['previous','operateDescription','channel_status','next','lastOperator']  # fields have in the hash  
  5. arr_del=['lastOperator','next']  # Don't have in the arr_need  
  6. hash_after=StringArray.hsh_delete_if(hash,arr_need,arr_del)   =>  {"previous"=>nil"operateDescription"=>nil"channel_status"=>["E""R""P""Q"]}  
  7. =end  
  8.   def self.hsh_delete_if(hash,arr_need,arr_del)  
  9.     hash.delete_if do |key,value|  
  10.       !arr_need.include?(key) or arr_del.include?(key)  
  11.     end   
  12.     return hash  
  13.   end  
  14.   
  15. =begin  
  16. # delete array value if it not in `arr_need` and it exists in `arr_del` value  
  17. hash=["previous""operateDescription""channel_status""next""lastOperator"]  
  18. arr_need=['previous','operateDescription','channel_status','next','lastOperator']  # fields have in the hash  
  19. arr_del=['lastOperator','next']  # Don't have in the arr_need  
  20. hash_after=StringArray.arr_delete_if(hash,arr_need,arr_del)   =>  {"previous"=>nil"operateDescription"=>nil"channel_status"=>["E""R""P""Q"]}  
  21. =end  
  22.   def self.arr_delete_if(array,arr_need,arr_del)  
  23.     array.delete_if do |value|  
  24.       !arr_need.include?(value) or arr_del.include?(value)   
  25.     end  
  26.     return array  
  27.   end  
分享到:
评论
2 楼 xu_ch 2009-06-15  
有没有helper?
1 楼 xu_wccq 2007-10-31  
为方法定义别名:

def create
......................
end

alias_method :self_create, :create
alias_method :delegate_create, :create

相关推荐

Global site tag (gtag.js) - Google Analytics