[#408634] How do I make lots of classes aware of each other? — "Andrew S." <lists@...>

I'm apparently missing something fundamental in my knowledge of classes

10 messages 2013/07/02

[#408712] Ruby web service with REST support — "Shubhada S." <lists@...>

Hi All,

17 messages 2013/07/05

[#408812] create variables depending on counter — stefan heinrich <lists@...>

Hi community,

21 messages 2013/07/09

[#408854] execute commands within SMTP email code: send content in variables and not actual variables — dJD col <lists@...>

I am trying to send an email using the code below. I am able to send the

9 messages 2013/07/10

[#409031] tap { break } idiom deserves its own Kernel method? — Andy Lowry <lists@...>

I use this idiom from time to time:

13 messages 2013/07/22

[#409072] Link To Masses Of External Data In Openoffice? — "Austin J." <lists@...>

This is what I want to do.

19 messages 2013/07/23
[#409102] Re: Link To Masses Of External Data In Openoffice? — Tamara Temple <tamouse.lists@...> 2013/07/24

[#409103] Re: Link To Masses Of External Data In Openoffice? — "Austin J." <lists@...> 2013/07/25

tamouse m. wrote in post #1116598:

[#409122] Re: Link To Masses Of External Data In Openoffice? — Tamara Temple <tamouse.lists@...> 2013/07/26

[#409142] Re: Link To Masses Of External Data In Openoffice? — "Austin J." <lists@...> 2013/07/26

tamouse m. wrote in post #1116750:

[#409073] class <=> module — Bráulio Bhavamitra <lists@...>

Hello all,

17 messages 2013/07/23

[#409104] Ruby newbie question on Methods (NoMethoderror) — "Crispian A." <lists@...>

I have recently started learning ruby and so I am writing a small little

10 messages 2013/07/25

[#409170] Working through Ch.10 for learning to program 2.0 (Chris Pine) — JD JD <lists@...>

So, I have been working through this book, and have been doing ok up

33 messages 2013/07/28
[#409195] Re: Working through Ch.10 for learning to program 2.0 (Chris Pine) — Harry Kakueki <list.push@...> 2013/07/29

I tried this and came up with a one-liner that seems to do it. It sorts the

[#409258] WATIR - ScriptError popup on IE - Unable to get rid of! — Graeme Halls <lists@...>

I am new to Ruby & Watir, and I am having a nightmare with IE and Script

11 messages 2013/07/31

Re: Well-Grounded Rubyist -- Person class implementation in chapter 4 question

From: Gleb Posobin <posobin@...>
Date: 2013-07-09 07:52:19 UTC
List: ruby-talk #408799
Firstly, the error you are receiving has been explained by Sam. Now, to the
interesting part:
Code in 4.13 defines method_missing for Person class. This method gets run
if you call some undefined method in the class. So, for example, if you do
Person.blablabla("argument", 2), ruby looks in the Person's class methods
(not instance methods, because blablabla was sent to the class itself, not
to the instance of it) for a method with name "blablabla". It does not find
such method, do then it looks whether the Person class has method_missing
method defined. It does, so ruby calls Person.method_missing(:blablabla,
"argument", 2), passing the parameters we called "blablabla" with to the
method_missing. Ruby puts name method to the "m" variable and the rest of
the arguments - to the args array (because it's marked with asterisk - so
called splat method, you can read a little bit about it
here<http://endofline.wordpress.com/2011/01/21/the-strange-ruby-splat/>).
Then, ruby converts :blablabla symbol to string and checks whether it
starts with "all_with_". It doesn't, so ruby calls super, which means "call
parent class' method with the same name of the current one and pass it the
same arguments". Now, let's have a look at what happens if we call
Person.all_with_name("Julia"). Method Person.all_with_name is not defined,
so ruby goes to method_missing for help. This time method's name starts
with "all_with_", so ruby extracts the rest of the method's name after
"all_with_" to attr variable. Now, ruby checks whether Person class has
instance method with the name stored in newly created attr variable (which
now contains "name"). Then ruby finds all persons from PEOPLE array, who
have name equal to the args[0], which is first argument passed to the
Person.all_with_name. "person.send" is another way of calling an instance
method, e.g. person.send("haha") is the same as person.haha. Docs for
object.send is here <http://ruby-doc.org/core-2.0/Object.html#method-i-send>.
So, at last ruby finds all persons, whose name includes "Julia" as a
substring. And, as this PEOPLE.find_all is the last line in the method to
be executed, method_missing returns an array of found persons.
P.S. If we called Person.all_with_blablabla, we would get ArgumentError
exception, because Person does not have instance method blablabla defined,
which is checked in line 6 of 4.13 gist.
P.P.S. I am myself not really experienced rubyist, so my explanation may
contain some mistakes. Be sure to correct them if you notice.
P.P.P.S. Also, my English is far from perfect, sorry for that.


2013/7/9 Arslan Farooq <lists@ruby-forum.com>

> Hi,
>
> I am new to programming and new to Ruby.
>
> I have been studying Ruby using David Black's book, and I need bit of
> help with this:
>
> Code in listing 4.11, 4.12 and 4.13 needs to be pieced together (I
> think) to do something useful. However it's a bit confusing... whether
> they are meant to just studied as examples separately, or whether I am
> supposed to piece them together. Please have a look at the code:
>
> https://gist.github.com/arslanfarooq/54ac5aa7796385e4a063
>
> If you could explain a bit what's going on in 4.13 that would be great.
>
> On my computer I have combined the code in one file "person_class.rb"
> and I am getting this error:
>
> person_class.rb:17:in `has_friends': undefined method `<<' for
> nil:NilClass (NoMethodError) from person_class.rb:43:in `<main>'
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

In This Thread

Prev Next