かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

[Ruby]問題でーすに答えてみる

前回は、C++で解答の条件を満たしてなかった。
でも、出題されてるエントリを見てると自動生成で実世界での用件を満たせそうだ。
XSLを使ってかかれてたので、Rubyを使って書いてみた。

require 'erb'
require 'yaml'

yaml_data = <<YAML
- {name: ParentA, child_name: ChildA, message: A家は塾に通う}
- {name: ParentB, child_name: ChildB, message: B家はパパが先生}
YAML

template = ERB.new <<TEMPLATE, nil, "-"
namespace Answer1
{
<%- src.each do |clazz| -%>
  class <%=clazz["name"]%>
  {
    public List<<%=clazz["child_name"]%>> Children = new List<<%=clazz["child_name"]%>>();
    private static string whereStudy = "<%=clazz["message"]%>";

    public void Add(<%=clazz["child_name"]%> child)
    {
      Children.Add(child);
    }

    public void Cooking()
    {
      Console.WriteLine("<%=clazz["name"]%>:Cooking()");
    }

    public class <%=clazz["child_name"]%>
    {
      public void Study()
      {
        Console.WriteLine("<%=clazz["message"]%>:Study():" + whereStudy);
      }
    }
  }
<%- end -%>
}
TEMPLATE

src = YAML::load(yaml_data)
template.run binding

YAMLで自動生成するクラスのメタデータ?を定義してERBでさくっと生成してみる。
実行すると下のようになって、たぶんもとのMainを書き換えなくても動くかなぁ…?

namespace Answer1
{
  class ParentA
  {
    public List<ChildA> Children = new List<ChildA>();
    private static string whereStudy = "A家は塾に通う";

    public void Add(ChildA child)
    {
      Children.Add(child);
    }

    public void Cooking()
    {
      Console.WriteLine("ParentA:Cooking()");
    }

    public class ChildA
    {
      public void Study()
      {
        Console.WriteLine("A家は塾に通う:Study():" + whereStudy);
      }
    }
  }
  class ParentB
  {
    public List<ChildB> Children = new List<ChildB>();
    private static string whereStudy = "B家はパパが先生";

    public void Add(ChildB child)
    {
      Children.Add(child);
    }

    public void Cooking()
    {
      Console.WriteLine("ParentB:Cooking()");
    }

    public class ChildB
    {
      public void Study()
      {
        Console.WriteLine("B家はパパが先生:Study():" + whereStudy);
      }
    }
  }
}

投稿日時 : 2008年3月5日 0:31

Feedback

# re: [Ruby]問題でーすに答えてみる 2008/03/05 0:35 かずき

あぁぁぁぁorz
一部間違えてたぁ。

Rubyコードの32行目
誤:Console.WriteLine("<%=clazz["message"]%>:Study():" + whereStudy);
正:Console.WriteLine("<%=clazz["child_name"]%>:Study():" + whereStudy);

これを直すと実行結果のStudyメソッドのWriteLineの中がChildAとChildBになる。
ふぅ失敗失敗。

# 問題でーす!の回答でーす! 2008/03/05 11:05 myugaruの色々構想中・・・!

問題でーす!の回答でーす!

# バーバリー コート レディース 2012/11/06 18:55 http://burberry.suppa.jp/

今日は よろしくお願いしますね^^すごいですね^^

# Nike Air Jordan 9 Retro 2012/12/08 7:10 http://suparjordanshoes.webs.com/

Appreciate it for helping out, wonderful information.

# longchamp pliage hobo 2012/12/15 15:41 http://www.soldesacslongchamp.info/category/sacs-l

I use earbuds out and about because within their portability, even though I prefer over the particular ear.

# handbags burberry uk 2012/12/16 3:54 http://www.burberryuksale.co/2012-burberry-handbag

I am sure I definitely will visit the place repeatedly soon.

# pliage longchamp outlet 2012/12/16 21:22 http://www.sacslongchamp2012.info/longchamps-darsh

Looking onward to reading through more!

# burberry outlet 2012/12/17 7:25 http://www.burberryuksale.org/category/burberry-sa

I literally like em. It's in contrast to you utilize these out walking around town individuals.

# isabel marant sneakers soldes 2012/12/17 18:46 http://www.isabelmarant-stores.com/category/isabel

Great short article, it's advantageous information.

# burberry outlet 2012/12/17 20:38 http://www.burberryuksale.org/category/burberry-uk

That's what exactly earbuds can be for.

# トリーバーチ 靴 2012/12/17 20:43 http://www.torybruchjp.info/category/トリーバーチ-靴

Nobody is likely to see someone to mug you from your basement.

# michael kors soldes 2012/12/21 3:40 http://sacmichaelkors.monwebeden.fr

If a person's photostream carries photos the fact that - whether or not good and / or not : triggered a spirited comments¡ä bond.

# http://www.destockchinefr.fr/veste-marque-pas-cher/veste-ed-hardy-pas-cher/ 2013/01/13 5:05 http://www.destockchinefr.fr/veste-marque-pas-cher

Romance is usually fallible during the labor and birth, nonetheless ages more as we grow older when it is adequately feasted.
http://www.destockchinefr.fr/veste-marque-pas-cher/veste-ed-hardy-pas-cher/ http://www.destockchinefr.fr/veste-marque-pas-cher/veste-ed-hardy-pas-cher/

# http://www.destockchinefr.fr/nike-shox-pas-cher/nike-shox-nz-2-pas-cher/ 2013/01/13 5:05 http://www.destockchinefr.fr/nike-shox-pas-cher/ni

Friendly relationship may possibly be the Coptis trifolia groenlandica the fact that neckties our minds with all the different marketplace.
http://www.destockchinefr.fr/nike-shox-pas-cher/nike-shox-nz-2-pas-cher/ http://www.destockchinefr.fr/nike-shox-pas-cher/nike-shox-nz-2-pas-cher/

# destockchine 2013/01/13 12:20 http://www.destockchinefr.fr/lunettes-marque-pas-c

This most inopportune process to miss anybody is planned to be relaxing top close to consumers with the knowledge yourrrre able to‘MT encourage them.
destockchine http://www.destockchinefr.fr/lunettes-marque-pas-cher/lunettes-ray-ban-pas-cher/

# lunettes ray ban 2013/03/04 23:27 http://www.f77.fr/

Assume‘capital t trash some time about a male/lovely lady,who else isn‘capital t ready to trash these moment done to you. lunettes ray ban http://www.f77.fr/

# lunettes oakley 2013/03/04 23:28 http://www.g33.fr/

Need not make friends which can be snug to get along with. Connect with others which will coerce a person to pry your true self on. lunettes oakley http://www.g33.fr/

# c55.fr 2013/03/04 23:29 http://www.c55.fr/

Relationships go on every time just about every roommate thinks about he's a slight high quality above the several. c55.fr http://www.c55.fr/

# www.jordanretro3air.com 2013/03/05 0:46 http://www.jordanretro3air.com/

Add‘p fritter away your energy and time within a men/lovely lady,exactly who isn‘p prepared fritter away a person's precious time you. www.jordanretro3air.com http://www.jordanretro3air.com/

# air jordan retro 7 olympic 2013/03/05 0:47 http://www.jordanretro7air.com/

Say you decided to would preserve your answer from the an enemy, determine keep in mind this will not the buddy. air jordan retro 7 olympic http://www.jordanretro7air.com/

# K77.fr 2013/03/05 0:48 http://www.k77.fr/

You shouldn't discuss about it your trusty felicity one fewer happi as compared with your family. K77.fr http://www.k77.fr/

# casquette snapback 2013/03/13 12:18 http://www.a77.fr/

Cherish is considered the hectic anxiety to the time therefore the increase of whatever we each real love. casquette snapback http://www.a77.fr/

# Code Promo Spartoo 2013/03/14 9:20 http://www.c88.fr/

Valid association foresees the needs of all the other in preference to proclaim its acquire. Code Promo Spartoo http://www.c88.fr/

# casquette new era 2013/03/16 10:09 http://www.a44.fr/

Do not make friends which have been cosy to get along with. Connect with others who will power to jimmy by yourself up. casquette new era http://www.a44.fr/

# casquette supreme 2013/03/17 7:02 http://www.b44.fr/

Happen to be endure whenever any coworker feels they have hook transcendency during the other sorts of. casquette supreme http://www.b44.fr/

# Destockchine 2013/03/18 8:25 http://www.ruenike.com/autres-c-25.html/

Satisfaction is a scent you cannot pour attached to other ones lacking applying for a couple falls attached to your spouse. Destockchine http://www.ruenike.com/autres-c-25.html/

# destockmania 2013/03/18 8:30 http://www.ruenike.com/foot-c-10.html/

The truth acquaintanceship foresees the needs of other sorts of and not exclaim you'll find it purchase. destockmania http://www.ruenike.com/foot-c-10.html/

# casquette raiders 2013/03/23 21:26 http://e77.fr/

Assume‘g trash your time , effort more than boyfriend/lover,what individuals isn‘g happy to trash his / her occasion giving you. casquette raiders http://e77.fr/

# destockchine 2013/03/25 2:35 http://c99.fr/

Love, camaraderie, prize, will not link up visitors over a normal hate designed for point. destockchine http://c99.fr/

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

A fact affinity foresees the requirements of a few other rather than just laud its special. rueducommerce http://ruezee.com/

# brandalley 2013/04/07 12:05 http://rueree.com/

If you happen to would probably choosing the best method due to an opponent, notify them don't a. brandalley http://rueree.com/

# tati 2013/04/07 20:04 http://ruenee.com/

By which there's always relationship getting enjoy, it'll be enjoy getting relationship. tati http://ruenee.com/

# sarenza soldes 2013/04/07 20:21 http://ruemee.com/

Where exactly there could be a bonded relationship whilst not having like, you'll see like whilst not having a bonded relationship. sarenza soldes http://ruemee.com/

# bszsiPtfZPxqiQs 2018/12/17 8:33 https://www.suba.me/

JDF95L Many thanks for sharing this very good article. Very inspiring! (as always, btw)

# WdFiIiQcfPwLdWuUF 2018/12/19 23:49 https://www.suba.me/

yLUOnn This is a set of phrases, not an essay. you are incompetent

# Hey there just wanted to give you a brief heads up and let you know a few of the images aren't loading properly. I'm not sure why but I think its a linking issue. I've tried it in two different internet browsers and both show the same results. 2019/04/19 5:58 Hey there just wanted to give you a brief heads up

Hey there just wanted to give you a brief heads up and let you know a few of the images aren't loading properly.
I'm not sure why but I think its a linking issue. I've tried it in two
different internet browsers and both show the same results.

# Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but instead of that, this is great blog. A great read. I will definitely 2019/07/20 17:47 Its like you read my mind! You appear to know so m

Its like you read my mind! You appear to know so much about this, like you wrote the book
in it or something. I think that you can do with some pics to drive
the message home a bit, but instead of that, this
is great blog. A great read. I will definitely be
back.

タイトル
名前
Url
コメント