[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[SOLUTION] metakoans.rb (#67

Florian Groß

2/19/2006 2:22:00 PM

8 Answers

James Gray

2/19/2006 5:15:00 PM

0

On Feb 19, 2006, at 8:21 AM, Florian Groß wrote:

> Ruby Quiz wrote:
>
>> # metakoans.rb is an arduous set of exercises designed to stretch
>> # meta-programming muscle. the focus is on a single method
>> 'attribute' which
>> # behaves much like the built-in 'attr', but whose properties
>> require delving
>> # deep into the depths of meta-ruby.
>
> Here we go then.

Here's what I coded up, back when Ara sent me the quiz. Everyone
else found much prettier code though. ;)

James Edward Gray II

#!/usr/local/bin/ruby -w

class Module
def attribute( name, &block )
if name.is_a? Hash
name.each do |attr_name, default|
define_method(attr_name) do
if instance_variables.include?("@#{attr_name}")
instance_variable_get("@#{attr_name}")
else
default
end
end

define_method("#{attr_name}=") do |value|
instance_variable_set("@#{attr_name}", value)
end

define_method("#{attr_name}?") do
send(attr_name) ? true : false
end
end
elsif block
define_method(name) do
if instance_variables.include?("@#{name}")
instance_variable_get("@#{name}")
else
instance_eval(&block)
end
end

define_method("#{name}=") do |value|
instance_variable_set("@#{name}", value)
end

define_method("#{name}?") do
send(name) ? true : false
end
else
define_method(name) do
instance_variable_get("@#{name}")
end

define_method("#{name}=") do |value|
instance_variable_set("@#{name}", value)
end

define_method("#{name}?") do
send(name) ? true : false
end
end
end
end



Ryan Leavengood

2/19/2006 7:26:00 PM

0

Here is mine. Nothing too extraordinary or different than other
solutions, but it is fairly short and readable.

class Module
def attribute(x, &block)
name, value = x.to_a[0] # produces a warning is x is a symbol
ivar = "@#{name}"
define_method(name) do
if instance_variables.include?(ivar)
instance_variable_get(ivar)
else
value || (instance_eval &block if block)
end
end
attr_writer name
define_method("#{name}?") { !!send(name) }
end
end

As others have said, this was a cool quiz. While the meta-programming
part was interesting, I think this quiz is a better advertisement for
test-first coding than anything! Very cool and fun test suite. Thanks
Ara.

Ryan


Michael Ulm

2/20/2006 2:39:00 PM

0

Christian Neukirchen wrote:

> Florian Groß <florgro@gmail.com> writes:
>
>
>>Ruby Quiz wrote:
>>
>>
>>> # metakoans.rb is an arduous set of exercises designed to stretch
>>> # meta-programming muscle. the focus is on a single method 'attribute' which
>>> # behaves much like the built-in 'attr', but whose properties require delving
>>> # deep into the depths of meta-ruby.
>>
>>Here we go then.
>>
>>I have a golfed (8 lines) and a regular solution (21 lines) and
>>generally do the simplest thing that makes the tests work.
>>
>>Got it to run in two tries by the way. I failed the fourtytwo test at
>>first. After that everything worked fine. :)
>
>
> Here's my solution, done in two tries too. Took about 15 minutes, I
> think.
>
>
> class Module
> def attribute(a, &block)
> if a.kind_of? Hash
> a, default = a.to_a.first
> else
> default = nil
> end
>
> a = a.to_sym
> ivar = "@#{a}"
>
> define_method(a) {
> if instance_variables.include? ivar
> instance_variable_get ivar
> else
> block ? instance_eval(&block) : default
> end
> }
> define_method("#{a}=") { |v| instance_variable_set ivar, v }
> define_method("#{a}?") { !!__send__(a) }
> end
> end
>

This demonstrates a problem I have with many solutions -- taking
Florians as an example.

class Test
attribute 'a'
end

tst.a? # => false OK
tst.a = false # => false OK
tst.a? # => false This should be true


Regards,

Michael

--
Michael Ulm
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: michael.ulm@isis-papyrus.com
Visit our Website: www.isis-papyrus.com

---------------------------------------------------------------
This e-mail is only intended for the recipient and not legally
binding. Unauthorised use, publication, reproduction or
disclosure of the content of this e-mail is not permitted.
This email has been checked for known viruses, but ISIS accepts
no responsibility for malicious or inappropriate content.
---------------------------------------------------------------


Michael Ulm

2/20/2006 2:41:00 PM

0

Michael Ulm wrote:

--snip--
>
> This demonstrates a problem I have with many solutions -- taking
> Florians as an example.
>
--snip--

s/Florian/Christian/

So sorry,

Michael


--
Michael Ulm
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: michael.ulm@isis-papyrus.com
Visit our Website: www.isis-papyrus.com

---------------------------------------------------------------
This e-mail is only intended for the recipient and not legally
binding. Unauthorised use, publication, reproduction or
disclosure of the content of this e-mail is not permitted.
This email has been checked for known viruses, but ISIS accepts
no responsibility for malicious or inappropriate content.
---------------------------------------------------------------


James Gray

2/20/2006 2:46:00 PM

0

On Feb 20, 2006, at 8:38 AM, Michael Ulm wrote:

> This demonstrates a problem I have with many solutions -- taking
> Florians as an example.
>
> class Test
> attribute 'a'
> end
>
> tst.a? # => false OK
> tst.a = false # => false OK
> tst.a? # => false This should be true

I believe you are making the mistake that you expect attr_meth?() to
tell you if an attribute is set. That's not the intention. attr_meth
() returns true if the attribute holds a true value.

I know this because I made the exact same mistake and Ara corrected
me. ;)

James Edward Gray II


Michael Ulm

2/20/2006 2:54:00 PM

0

James Edward Gray II wrote:

> On Feb 20, 2006, at 8:38 AM, Michael Ulm wrote:
>
>> This demonstrates a problem I have with many solutions -- taking
>> Florians as an example.
>>
>> class Test
>> attribute 'a'
>> end
>>
>> tst.a? # => false OK
>> tst.a = false # => false OK
>> tst.a? # => false This should be true
>
>
> I believe you are making the mistake that you expect attr_meth?() to
> tell you if an attribute is set. That's not the intention. attr_meth
> () returns true if the attribute holds a true value.
>
> I know this because I made the exact same mistake and Ara corrected
> me. ;)

I just checked the metakoans.rb file, and see that you are right:

# o.a? # query - true if @a

Had I defined the interface, this would behave differently. I guess
this shows that I am not enlightened yet.

Regards,

Michael


--
Michael Ulm
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: michael.ulm@isis-papyrus.com
Visit our Website: www.isis-papyrus.com

---------------------------------------------------------------
This e-mail is only intended for the recipient and not legally
binding. Unauthorised use, publication, reproduction or
disclosure of the content of this e-mail is not permitted.
This email has been checked for known viruses, but ISIS accepts
no responsibility for malicious or inappropriate content.
---------------------------------------------------------------


Christian Neukirchen

2/20/2006 4:24:00 PM

0

Michael Ulm <michael.ulm@isis-papyrus.com> writes:

> Michael Ulm wrote:
>
> --snip--
>> This demonstrates a problem I have with many solutions -- taking
>> Florians as an example.
>>
> --snip--
>
> s/Florian/Christian/
>
> So sorry,

Umm, the quiz says:

# o.a? # query - true if @a

If @a is nil (or unset) or false, a? is false; else, a? is true.
YMMV, but that's what I think makes most sense.

> Michael
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Jeremy Hinegardner

2/21/2006 7:04:00 AM

0

Great quiz Ara, and here's my solution. For some reason I wasn't using
instance_eval correctly, so I ended up defining an initalizer method for
the attribute. Also, I seem to have implemented the attr? method in a
manner different than most folks.

enjoy,

-jeremy

--
========================================================================
Jeremy Hinegardner jeremy@hinegardner.org