かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

オブジェクト指向におけるFizzBuzz問題をやってみた その2

突込みが何個かあったのでそれも踏まえてやってみた。

モトネタ
http://blogs.wankuma.com/episteme/archive/2007/11/08/106927.aspx
http://blogs.wankuma.com/kazuki/archive/2007/11/08/107032.aspx

課題

  1. Animalがnewできるじゃん?

というわけでさくっと片付けよう。

main.rb
require 'animal.rb'

animal = nil
animal = Dog.new
animal.sound
animal.count = 3
animal.sound
animal = Cat.new
animal.sound

animal.rb
module Animal
  attr_accessor :count

  def initialize
    self.count = 1
  end

  def sound
    self.count.times { cry }
    puts
  end
 
end

class Dog
  include Animal
  def cry
    print "わん"
  end
end

class Cat
  include Animal
  def cry
    print "にゃー"
  end
end

こんなもん???
これで、Animal.newもできない!!!

因みにRubyみたいなDuck Typing出来る言語だと、鳴ける?鳴けない?はこういう感じで回避かなぁ?

# 魅惑のナメクジさん
class Slug
  include Animal
  def eat
    puts "泥をむしゃむしゃと食べます"
  end
end

s = Slug.new
s.sound if s.respond_to? :cry # 鳴けたら声聞かせて
s.eat   if s.respond_to? :eat # 食べれたら食べて

respond_to?メソッドで、応答するかどうかを聞けるので確認してから伺えばよろしい。
突然変異で鳴けるナメクジもOK

s = Slug.new

class << s
  def cry
    print "ぬめ"
  end
end

s.count = 3
s.sound

これを実行すると「ぬめぬめぬめ」と表示される。

投稿日時 : 2007年11月9日 21:55

Feedback

# re: オブジェクト指向におけるFizzBuzz問題をやってみた その2 2007/11/10 8:53 R・田中一郎

ここであえてナメクジを登場させる点は、非常にポイント高いですよねw

# sac longchamp pas cher 2012/10/17 21:57 http://www.sacslongchamppascher2013.com

Absolutely indited subject material , thanks for selective information .

# Cheap Air Jordan 2012 Retro 2012/12/08 11:18 http://suparjordanshoes.webs.com/

Appreciate it for helping out, good information. "It does not do to dwell on dreams and forget to live." by J. K. Rowling.

# louis vuitton geldbörse 2012/12/12 11:44 http://www.louisvuittontascheneu.com/

Don‘h throw away as well as in a boyfriend/wife,people who isn‘h prepared to throw away their particular moment done to you.

# longchamp pliage 2012/12/14 22:55 http://www.longchampbagoutlet.info/category/longch

gripping fields of feed-back bursting from your photos.

# sacs longchamps 2012/12/15 16:00 http://www.sacslongchamp2012.info/pliage-longchamp

I'm sure impressed by the quality of information about this website. There are a great deal of good resources here.

# burberry sale 2012/12/16 4:33 http://www.burberryuksale.info/category/bags-burbe

You quite know a stuff...

# sacs longchamp soldes 2012/12/16 21:58 http://www.saclongchampachete.com/category/sacs-lo

You genuinely know your own stuff...

# longchamp solde 2012/12/17 8:03 http://www.longchampfr.info/category/sac-longchamp

I won't know... there's something tacky related to owning Ferrari branded stuff like this.. unless you additionally own an absolute Ferrari.

# bags burberry 2012/12/17 21:11 http://www.burberryuksale.org/category/bags-burber

keep the good give good results!

# michael kors paris 2012/12/18 22:19 http://sac-michael-kors.webnode.fr/sur-michael-kor

this is something we've never ever previously read.

# michael kors watches 2012/12/21 5:51 http://michaelkorswatch.webeden.co.uk

If some people sound excellent I'd 100 % wear these at your home.

# Jordan Retro 7 Raptors 2013/03/06 21:22 http://www.jordanretro7air.com/

Low man or woman is really worth your entire tears, as well as the individual who might be won‘r give you war cry. Jordan Retro 7 Raptors http://www.jordanretro7air.com/

# reduction la redoute 2013/03/06 21:24 http://www.k77.fr/

Pleasure is actually a cologne you simply can't plastic bottles found on most people getting choosing a not many declines found on yourself. reduction la redoute http://www.k77.fr/

# destock chine 2013/03/06 21:43 http://www.c55.fr/

Add‘l fritter away some time about a person/sweetheart,who also isn‘l happy to fritter away his precious time done to you. destock chine http://www.c55.fr/

# destock jeans 2013/03/06 21:45 http://www.g77.fr/

Affection may be the primary rational coupled with great solution on people reputation. destock jeans http://www.g77.fr/

# marqueBon 2013/03/06 21:46 http://www.g33.fr/

Just because someone doesn‘p love you how i desire them in order to really,doesn‘p require they'll preceptor‘p love you system they want. marqueBon http://www.g33.fr/

# casquette supreme 2013/03/06 21:48 http://www.b66.fr/

For no reason lour, regardless of whether you can find yourself depressing, as you never know who has plummeting excited about your smirk. casquette supreme http://www.b66.fr/

# casquette ny 2013/03/16 10:14 http://www.a44.fr/

Do not ever glower, when you , yourself are downcast, for those who can't say for sure who's going to be falling out excited about an individual's teeth. casquette ny http://www.a44.fr/

# destock mode 2013/03/17 10:15 http://www.b77.fr/

Take pleasure in could possibly be the well known challenge of the lifestyles also , the expansion of what we will adoration. destock mode http://www.b77.fr/

# usine23 2013/03/25 2:36 http://e55.fr/

Our worst type of procedure to neglect one is to be sitting straight definitely all of them being familiar with you are‘big t encourage them. usine23 http://e55.fr/

# destockchine 2013/03/25 5:19 http://c99.fr/

Perhaps Lord likes individuals in order to reach a couple different bad men or women previous to discussion the right choice, make sure after we ultimately fulfill the people, you will figure out how to sometimes be head over heels. destockchine http://c99.fr/

# 百家乐 2013/04/03 7:12 http://tt6262.com/

Exactly where you will find wedlock with out really enjoy, you will encounter really enjoy with out wedlock. 百家? http://tt6262.com/

# rueducommerce 2013/04/07 7:21 http://ruezee.com/

Found in wealth a contacts comprehend states; through adversity we know a contacts. rueducommerce http://ruezee.com/

# ruedee.com 2013/04/07 17:26 http://www.ruedee.com/

Wear‘metric ton check out so hard, a great items are available while you lowest foresee them how to. ruedee.com http://www.ruedee.com/

# ruemee.com 2013/04/08 4:32 http://ruemee.com/

Do not talk about all of your well-being to just one significantly grateful when compared with for yourself. ruemee.com http://ruemee.com/

# desigual 2013/04/08 15:46 http://ruenee.com/

Mainly because someone doesn‘g adore you job long for them so that you can,doesn‘g make the companies add‘g adore you operating they have perhaps. desigual http://ruenee.com/

# HiWPquvcDXPVeYbvm 2015/05/04 3:38 horny

tDeMOp http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com

# EeuGUrFhfcIbdpySH 2015/05/19 10:50 Dewayne

When can you start? http://www.smhv.nl/faq cheap lovegra uk On this week's Daily News Fifth Yankees Podcast, Mark Feinsand sits down with outfielder Vernon Wells to discuss the Yankees' fading postseason hopes, the tough road trip and what the team's recent stumble means going forward.

# JnkLgTjoPmPa 2015/05/19 10:50 Morris

Through friends http://www.smhv.nl/over-smhv beli caverta Kevin Brito, 15, who plays on the Inwood youth league�s Yankees squad, doesn�t care either way. He said Rodriguez� performance-enhanced career had left him disenchanted with big league stars in general.

タイトル  
名前  
Url
コメント