« 4章.参照渡しと値渡し (その3) | メイン | [番外]咳さんに褒められていた件 »

4章.参照渡しと値渡し (その4)

4章.参照渡しと値渡し (その4)

今回は参照渡しをしてみます。ちょっと本に戻ります。p.57の「4.1.2 dRubyでは?」からです。が、Marshalのあたりは、同じようなことを既に確認したような気がしますのでここでは省略します。本に沿って試してみて下さいませ。ここではその続き「p.58 参照の値渡し」から読みます。

Marshal.loadによって直列化したオブジェクトをMarshal.dumpで復元するということは、つまりオブジェクトの複製を作ることにほかなりません。ではdRubyはいつも値渡しなのでしょうか?
実は当たっているとも言えるし違うとも言えます。 dRubyでの参照渡しは、元のオブジェクトの代わりに参照情報を含むオブジェクトを直列化することで実現されます。つまり、参照渡しは参照オブジェクトの値渡しなのです。逆に、もう一方の値渡しは単純にオブジェクトを直列化するだけです。

4.1.2 dRubyでは?

ふむふむ。

Hello, World.の実験で使ったDRbObjectを覚えていますか? DRbObjectは元のオブジェクトを参照する情報をもったオブジェクトです。 DRbObjectには目的にごとに二つの生成方法が用意されています。一つはDRbObject.new_with_uriを使ってURIからリモートオブジェクトへの参照を作ることです。もう一つはDRbObject.new(obj)のようにオブジェクトを与え自プロセスのオブジェクトへの参照を作ることです。

4.1.2 dRubyでは?

ふむふむふむ。

DRbObject.new(obj)によって、任意のオブジェクトへの参照オブジェクトを作ります。参照を作るということは、他のプロセスがこの参照をつかってメソッド呼び出しする可能性があるということです。ですから、オブジェクトへの参照を作る前に、DRb.start_serviceによってdRubyのサーバを自プロセスで起動しておかなくてはなりません。

4.1.2 dRubyでは?

ふむふむふむふむふむ!!(こればっか)

元の文章でも充分わかりやすいですが、整理します。ここ、すごい重要な気がします。


  1. dRubyは、通常は値渡しだが、参照渡しにすることも出来る

  2. dRubyでの参照渡しは、厳密に言うと参照オブジェクトの値渡しだ

  3. 参照オブジェクトとは、すなわちDRbObjectのこと

  4. DRbObjectにはふたつの使い道がある

  5. 4-1. DRbObject.new_with_uriを使ってURIからリモートオブジェクトへの参照を作る
    4-2. DRbObject.new(obj)のようにオブジェクトを与え自プロセスのオブジェクトへの参照を作る

  6. 今までは上 (4-1) の使い道でしか使ってなかった。

  7. DRbObject.new(obj)で参照を作るためには、クライアントであってもDRb.start_serviceしないと駄目

  8. なぜなら、そのオブジェクトを他からリモート呼び出しすることがあるから(オブジェクトの実体はクライアントにあるから)


すげ!ちょっと感動します。

確かめてみます。p.60に掲載されているように、ハッシュを公開するサーバーを用意します。

% irb --prompt simple -r drb/drb
>> front = {}
>> DRb.start_service(nil, front)
=> #
>> DRb.uri
=> "druby://yourhost:1426"

はい立ち上げてみました。

前回の値渡しの復習をかねて、文字列をハッシュ(フロントオブジェクト)に登録してみます。

there = DRbObject.new_with_uri('druby://yourhost:1426')
str = "Hello, World."
there[1] = str

これは勿論うまくいく。では次に参照を渡してみます。参照はDRbObjectのことです。

there = DRbObject.new_with_uri('druby://yourhost:1426')
str = "Hello, World."
there[1] = str
there[2] = DRbObject.new(str)

しかしこれではうまくいきません。もしこのまま実行すると

in `current_server': DRb::DRbServerNotFound (DRb::DRbServerNotFound)

というエラーが出てしまいます。
なぜなら、DRb.start_serviceをしていないからです。クライアントからオブジェクトの参照を放り込んでいるということは実体がクライアントにあるということです。なのでメッセージに応答するのはクライアントです。クライアントが他局からのメッセージ呼び出しを受け付けることができないと駄目なので、dRubyのサービスが必要です。これがDRb.start_serviceをしなければならない理由です。

DRb.start_service
there = DRbObject.new_with_uri('druby://yourhost:1426')
str = "Hello, World."
there[1] = str
there[2] = DRbObject.new(str)

これでうまくいきます。
今日はここまで。次回は今回やった参照渡しの部分をもうちょっと詳しく見るつもりです。

トラックバック

このエントリーのトラックバックURL:
http://www.grandnature.net/bin/mt-tb.cgi/65

コメント (57)

I’d have to give blessing with you here. Which is not something I usually do! I love reading a post that will make people think. Also, thanks for allowing me to comment!

I like what you guys are up also. Such smart work and reporting! Keep up the excellent works guys I’ve incorporated you guys to my blogroll. I think it'll improve the value of my site :)

I haven’t checked in here for a while because I thought it was getting boring, but the last several posts are good quality so I guess I’ll add you back to my everyday bloglist. You deserve it my friend :)

I’d be inclined to see eye to eye with you here. Which is not something I usually do! I really like reading a post that will make people think. Also, thanks for allowing me to comment!

Hey there! I know this is certainly considerably off theme but I was thinking which site platform are you using for this web page? I’m finding tired of Wordpress as I’ve had issues with hackers and I’m hunting at alternatives for another platform. I'd be awesome if you could stage me in the direction of a fantastic platform.

I'm extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you modify it yourself? Either way keep up the nice quality writing, it’s rare to see a nice blog like this one nowadays..

obviously like your web-site but you have to check the spelling on several of your posts. Many of them are rife with spelling problems and I find it very bothersome to tell the truth nevertheless I’ll certainly come back again.

F*ckin’ tremendous things here. I am very glad to see your post. Thanks a lot and i'm looking forward to contact you. Will you kindly drop me a mail?

This is the suitable blog for anyone who desires to seek out out about this topic. You notice so much its nearly exhausting to argue with you (not that I actually would need…HaHa). You undoubtedly put a new spin on a topic thats been written about for years. Great stuff, just nice!

It’s really a nice and helpful piece of information. I am glad that you shared this useful info with us. Please keep us informed like this. Thank you for sharing.

It’s onerous to search out educated people on this matter, but you sound like you already know what you’re speaking about! Thanks

I discovered your weblog website on google and check a number of of your early posts. Continue to keep up the superb operate. I just extra up your RSS feed to my MSN Information Reader. Searching for ahead to studying extra from you in a while!…

It’s arduous to find knowledgeable people on this matter, but you sound like you already know what you’re talking about! Thanks

I’ll right away snatch your rss as I can't find your email subscription hyperlink or e-newsletter service. Do you have any? Kindly permit me recognise so that I may just subscribe. Thanks.

of course like your web site but you have to check the spelling on several of your posts. A number of them are rife with spelling issues and I find it very bothersome to inform the reality however I’ll surely come back again.

hello there and thank you in your information – I’ve certainly picked up something new from proper here. I did then again expertise a few technical points the usage of this web site, as I experienced to reload the website lots of instances previous to I may just get it to load properly. I had been puzzling over in case your hosting is OK? No longer that I am complaining, however slow loading cases times will very frequently impact your placement in google and could damage your high quality ranking if advertising and ***********|advertising|advertising|advertising and *********** with Adwords. Anyway I’m including this RSS to my e-mail and can glance out for a lot extra of your respective fascinating content. Make sure you replace this again very soon..

Aw, this was a really nice post. In idea I wish to put in writing like this moreover – taking time and actual effort to make a very good article… but what can I say… I procrastinate alot and under no circumstances seem to get one thing done.

Your place is valueble for me. Thanks!…

I conceive other website proprietors should take this internet site as an model, very clean and excellent user genial design .

I’m definitely bookmarking this site. Actually nice articles. Do you advocate any other readings?

Although operating a enterprise sounds cool and all that. But the fact, I believe essential kind a agenda so that you simply don’t get too involved. I favored that stuff you mentioned “existence is stability”. Maybe hiring somebody do your paintings is a brilliant idea.

Hi there, simply changed into alert to your blog thru Google, and found that it is really informative. I am gonna be careful for brussels. I’ll appreciate if you proceed this in future. Many people will likely be benefited out of your writing. Cheers!

yes, i consider that these form of suggestions are important. however, that’s just a bit part in web optimization CMIIW.

I’m really loving the contents of your blog. Hopefully you keep posting regularly. Thanks.

I’m really loving the contents of your blog. Hopefully you keep posting regularly. Thanks.

I’m really loving the contents of your blog. Hopefully you keep posting regularly. Thanks.

Attempt to strive, that's the key to good fortune, giving up shouldn’t be incorporated on our dictionary (i believe).

That is the correct blog for anyone who wants to find out about this topic. You understand so much its virtually exhausting to argue with you (not that I really would need…HaHa). You undoubtedly put a new spin on a topic thats been written about for years. Great stuff, simply great!

I usually don’t put up in Blogs but your blog compelled me to, amazing work.. beautiful …

I believe, to be an excellent affiliate marketer is the hardest thing to do as a blogger, you should have enormous site visitors to get the attainable purchaser and in addition must to build your own viewers, end result honestly i’ve never made a single sale with my first weblog

It’s onerous to search out educated people on this topic, however you sound like you recognize what you’re speaking about! Thanks

ohh…nice submit however actually?/? :P

It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

Aw, this was a really nice post. In thought I would like to put in writing like this moreover – taking time and precise effort to make a very good article… however what can I say… I procrastinate alot and not at all seem to get something done.

you have a great blog here! would you like to make some invite posts on my blog?

I just wanted to type a note in order to say thanks to you for those amazing tricks you are posting on this site. My incredibly long internet investigation has at the end been recognized with pleasant facts and techniques to share with my friends and classmates. I would assert that we visitors actually are undoubtedly lucky to be in a magnificent community with many marvellous professionals with very beneficial opinions. I feel very much fortunate to have come across your entire web page and look forward to plenty of more awesome times reading here. Thanks a lot again for all the details.

Aw, this was a very nice post. In thought I wish to put in writing like this additionally – taking time and actual effort to make an excellent article… however what can I say… I procrastinate alot and by no means seem to get something done.

:O So mush Info :O … THis Is he MOst AMAzing SIte DUDe… :D

This is a very good tips especially to those new to blogosphere, brief and accurate information… Thanks for sharing this one. A must read article.

After all, what a great site and informative posts, I will upload inbound link – bookmark this web site? Regards, Reader.

You made some respectable points there. I looked on the web for the issue and located most individuals will go together with with your website.

It’s onerous to find educated folks on this topic, however you sound like you understand what you’re speaking about! Thanks

Hi there, just became alert to your blog through Google, and found that it's truly informative. I am gonna watch out for brussels. I’ll be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers!

I’m not that much of a online reader to be honest but your sites really nice, keep it up! I'll go ahead and bookmark your website to come back down the road. Many thanks

yes, i believe that those type of ideas are important. nonetheless, that’s just a bit half in web optimization CMIIW.

We're in powerful instances these days, financial is pretty bad. To gain visitors and readers, it’s really about putting high quality contents and fascinating articles.

This is a nice article. I've now left the rat race, never to seem back. You’re right that the one technique to make any first rate cash is by way of operating your individual enterprise!

I’m sure this may be amazingly helpful for the ones weblog via - all of those seem like pretty excellent ideas for approach spend a while together with your site.

I’d must examine with you here. Which isn't something I often do! I get pleasure from studying a put up that may make people think. Additionally, thanks for allowing me to remark!

攻めようにも勝手に進んでればやりようがないの。一応第三者機関の検査が基礎工事で1回入ってるはずなので それだけが頼りのような???

I just want to tell you that I'm newbie to blogging and really enjoyed this web site. Probably I’m want to bookmark your blog . You really have wonderful article content. Cheers for revealing your webpage.

I simply want to tell you that I am very new to blogs and definitely loved this web site. Likely I’m planning to bookmark your blog . You surely have beneficial articles and reviews. With thanks for revealing your blog site.

I’m undoubtedly bookmarking this site. Really great articles. Do you recommend another readings?

one of the best idea is 100% content. Use content material when you do your promotions. You’ve most likely seen in this weblog at any time when I do a weblog publish that just about simply recommends a services or products, all the time a few people don’t like that.

I’d must examine with you here. Which isn't one thing I normally do! I take pleasure in studying a post that will make people think. Additionally, thanks for allowing me to remark!

OMG, I’ve been seeking for this material all over the internet. thanks

コメントを投稿

About

2007年10月11日 10:08に投稿されたエントリーのページです。

ひとつ前の投稿は「4章.参照渡しと値渡し (その3)」です。

次の投稿は「[番外]咳さんに褒められていた件」です。

他にも多くのエントリーがあります。メインページアーカイブページも見てください。