krystal: プログラミング超初心者(文系)

2008-12-17 (Wed)たのしいRuby練習問題

P266 正規表現クラス



  • メルアドは「ローカルパート@ドメイン名」という形になってるから、ローカルパートを$1として、ドメイン名を$2として取得する正規表現を作る
str = "s.t.a.t.e_of_s.h.o.c.k123@hotmail.co.jp"
/(\w.*)@(\w.*)/ =~ str
local_part = $1
domain_name = $2
p local_part    #=>  "s.t.a.t.e_of_s.h.o.c.k123"
p domain_name   #=>  "hotmail.co.jp"


  • 「オブジェクト指向は難しい!なんて難しいんだ!」という文字列をgsubメソッドを使って「オブジェクト指向は簡単だ!なんて簡単なんだ!」という文字列に直す
str = "オブジェクト指向は難しい!なんて難しいんだ!"
p str.gsub(/難しい/, "簡単だ").gsub(/だん/, "なん")   #=>  "オブジェクト指向は簡単だ!なんて簡単なんだ!"


  • アルファベットとハイフンからなる文字列を与えれると、ハイフンで区切られた部分をCapitalizeするようなメソッドword_capitalizeを定義する
  • こんな感じで
p word_capitalize("in-reply-to")  #=> "In-Reply-To"
p word_capitalize("X-MAILER")  #=> "X-Mailer"
  • 私のコード
def word_capitalize(str)
  if /\w-*/ =~ str
    ary = Array.new
      ary << x.capitalize
p word_capitalize("out-of-the-box-to-the-sky")  #=> "Out-Of-The-Box-To-The-Sky"
p word_capitalize("GO-THE-DISTANCE")                 #=> "Go-The-Distance"


  • CD-ROMによる(3)の解答はこの2つ:
def word_capitalize(str)
  return str.split(/\-/).collect{|w| w.capitalize}.join('-')

p word_capitalize("in-reply-to") #=> "In-Reply-To"
p word_capitalize("X-MAILER")    #=> "X-Mailer"
def word_capitalize(str)
  ret = ""
  str.scan(/(\w+)(\W*)/){|word, no_word|
    ret << word.capitalize + no_word
  return ret

p word_capitalize("in-reply-to") #=> "In-Reply-To"
p word_capitalize("X-MAILER")    #=> "X-Mailer"

EmmlyEmmly2016/05/07 16:14here we love LOVE. The emotion, the actions, the rituals and detifinely the word. Whether written on a sign or splashed across a shoe (yes, please Mr. Louboutin) it’s one of the few times when kitsch

EssieEssie2016/05/09 10:37Oh, and Israel, we just criticise, but we never, ever <a href="http://ylolojtyov.com">âœonÂc€ndem”</a> Israel or, God Forbid, Zionism in principle.Anyone get the impression that Tex Tradd is a J Street Stooge?

StapradlyStapradly2018/01/28 02:03Buy Cialis Online Safely Priligy Testimonianze <a href=http://cial10mg.com>online pharmacy</a> Prix Du Viagra Ou Autre Viagra Soft Tabs Erfahrung Viagra Cialis

DiaobjeteDiaobjete2018/04/19 03:02Buy Doxycycline Online Safe <a href=http://tadalaffbuy.com>cialis</a> Tadalafil 40mg Acquistare Kamagra Thailandia Kamagra Dosis

DiaobjeteDiaobjete2018/05/03 06:38Levitra De Bayer Precios Effetto Viagra Donne <a href=http://cialibuy.com>cheapest cialis 20mg</a> Propecia Prix Canadien Over The Counter Deltasone Medication Buy Tadacip 20 Mg Cod Pharmacy Online

WilliamlopWilliamlop2019/02/02 09:56retail price for cialis
online apotheke cialis kaufen
<a href=https://greatwinesgrandhouses.com>buy Cialis 20 mg</a>
reasonable drug discounter, cialis
cialis 5 mg from the pill store
which companies sell cialis
cialis canadian pahrmcy
<a href=https://kellyannehulme.com>cialis</a>
cialis no prescriptino
best price for cialis 5mg

WilliamlopWilliamlop2019/02/02 09:57cheap samples cialis
cialis in sam club
<a href=https://kellyannehulme.com>buy cialis</a>
cialis not working first time
best price online cialis 10mg
selling cialis
cialis canandien
<a href=https://greatwinesgrandhouses.com>20 mg cialis</a>
cialis rezeptpflichtig
cialis cream online in canada

ArnoldferryArnoldferry2019/02/05 18:02tips for taking cialis
cialis directions
<a href="http://xcialisxx.com">Buy Cheap Cialis Online</a>
cialis gel canada
buy cialis real
<a href="http://xcialisxx.com">buy cialis</a>
pharmacy cadia cialis
cialis what is it
<a href="http://cialisfurr.com">Buy Cheap Cialis</a>

ArnoldferryArnoldferry2019/02/05 18:03cialis in der apotheke
cialis side effects alcohol
<a href="http://cialistlm.com">Buy Cheap Cialis</a>
new cialis pharmacy
5mg cialis review
<a href="http://xcialisxx.com">Buy Cheap Cialis Online</a>
overnight pharm cialis
free trial offer for cialis
<a href="http://cialistlm.com">Cheap Cialis</a>

StevdrucheStevdruche2019/09/16 22:06Cheap Tamoxifen Citrate Xlpharmacy <a href=http://asacdz.com>order cialis online</a> Precio Propecia 20 Mg Lioresal Medicament