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

字符串截取,
英文单词单数复数转换( 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  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值