[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: [QUIZ] metakoans.rb (#67

MenTaLguY

2/18/2006 7:03:00 PM

On Sat, 2006-02-18 at 23:41 +0900, Patrick Hurley wrote:
> I also have 13; however, two lines exceed 80 columns. Additionally, I
> have no support for multiple attributes on one line -- seems logical
> to have...

13 seems to be the lower limit without seriously golfing. My "best"
solution at this point runs about 18 lines, but it'd be 13 if I took out
all the blank lines and one line that makes it faster but doesn't affect
the semantics.

-mental



7 Answers

Kero van Gelder

2/18/2006 10:47:00 PM

0

>> I also have 13; however, two lines exceed 80 columns. Additionally, I
>> have no support for multiple attributes on one line -- seems logical
>> to have...
>
> 13 seems to be the lower limit without seriously golfing. My "best"
> solution at this point runs about 18 lines, but it'd be 13 if I took out
> all the blank lines and one line that makes it faster but doesn't affect
> the semantics.

Dunno about golfing, been snowboarding the whole week.

I'm near koan 8 with 28 lines, and I need sleep (failed at 7, while
wondering why the program knew I did [need sleep]; Then I solved 7 while
puzzling about 8, or actually ironing out an inconsistency in my program;
maybe I am already asleep)

Anyway, what I wanted to post about: in the description/code there are a few
asserts like this:
assert{ (o.a = nil) == nil }
which will always be asserted, since Ruby evaluates 'a = b' to b, no matter
what a is; specifically, it does not evaluate to the result of #a= in code
like 'o.a = b'; imho, this makes perfect sense for statements like 'a = b = c'

Code to show:

class X
attr_reader :ha
def ha=(val)
@ha = "not what you expect"
end
end
x = X.new
p x.ha = nil # => nil
p x.ha # => "not what you expect"
p (x.ha = nil) == nil # => true

Bye,
Kero.

PS: fun quiz!

Timothy Goddard

2/18/2006 10:55:00 PM

0

To take the focus off number of lines, let's take a look at bechmarks.
This was my benchmark script:

Benchmark::bm(12) do |x|
x.report('attr') {100000.times {c = Class.new {attr :foo, :bar}}}
x.report('attribute') {100000.times {c = Class.new {attribute :foo,
:bar}}}
end

And my results were:

user system total real
attr 3.720000 0.000000 3.720000 ( 3.724316)
attribute 25.750000 0.000000 25.750000 ( 25.790665)

It isn't that efficient, but I'd like to see how it compares.

Wilson Bilkovich

2/19/2006 1:31:00 AM

0

On 2/18/06, Timothy Goddard <interfecus@gmail.com> wrote:
> To take the focus off number of lines, let's take a look at bechmarks.
> This was my benchmark script:
>
> Benchmark::bm(12) do |x|
> x.report('attr') {100000.times {c = Class.new {attr :foo, :bar}}}
> x.report('attribute') {100000.times {c = Class.new {attribute :foo,
> :bar}}}
> end
>
> And my results were:
>
> user system total real
> attr 3.720000 0.000000 3.720000 ( 3.724316)
> attribute 25.750000 0.000000 25.750000 ( 25.790665)
>
> It isn't that efficient, but I'd like to see how it compares.
>

That's interesting. I didn't expect the difference to be quite this
big. attr is impressively fast.
user system total real
attr 7.250000 0.000000 7.250000 ( 7.250000)
attribute 30.110000 0.000000 30.110000 ( 30.172000)

I'm looking forward to seeing some of the other solutions, for sure.
I had my hands halfway around a faster way, but couldn't quite make it
work.


Harold Hausman

2/19/2006 4:01:00 AM

0

attr_* are written in c if I'm not mistaken. That might explain (at
least some of) the difference.

+1 on this being an excellent quiz. Thanks again Ara

-Harold

On 2/18/06, Eero Saynatkari <ruby-ml@magical-cat.org> wrote:
> On 2006.02.19 10:30, Wilson Bilkovich wrote:
> > On 2/18/06, Timothy Goddard <interfecus@gmail.com> wrote:
> > > To take the focus off number of lines, let's take a look at bechmarks

Greg Millam

2/19/2006 7:48:00 AM

0

MenTaLguY wrote:

> On Sat, 2006-02-18 at 23:41 +0900, Patrick Hurley wrote:
>
>> I also have 13; however, two lines exceed 80 columns. Additionally, I
>> have no support for multiple attributes on one line -- seems logical
>> to have...
>
>
>
> 13 seems to be the lower limit without seriously golfing. My "best"
> solution at this point runs about 18 lines, but it'd be 13 if I took out
> all the blank lines and one line that makes it faster but doesn't affect
> the semantics.


Mine's at 9 without any real golfing. No newlines though. Maximum line
length's 67, and that's the largest by far. I use "cond ? true : false"
4 times though, and that could be cleaner, I imagine.

However: While it passes all the tests, it might have some poor flaws in
real world use.

i.e:
* Anything with the same hash would have the same attributes. (i.e: 2
string objects containing the same text)
* Every instance of the class that uses the attribute is kept in
memory so long as the class is.
* foo? returns the value, instead of exact true/false. While this is
legit, it can't be used in (a.foo? == false) conditionals. But that's
unrubylike anyway :D

- Greg


Mike Harris

2/19/2006 10:31:00 AM

0

MenTaLguY wrote:

>On Sat, 2006-02-18 at 23:41 +0900, Patrick Hurley wrote:
>
>
>>I also have 13; however, two lines exceed 80 columns. Additionally, I
>>have no support for multiple attributes on one line -- seems logical
>>to have...
>>
>>
>
>13 seems to be the lower limit without seriously golfing. My "best"
>solution at this point runs about 18 lines, but it'd be 13 if I took out
>all the blank lines and one line that makes it faster but doesn't affect
>the semantics.
>
>-mental
>
>
>
>
>
Yea, I don't usually golf, so I'm torn as to what's reasonable and
what's golfing, in terms of line count. Right now I'm at 17, and I got
it down to 13 or 14 before changing it back cause I found the new code
aesthetically displeasing.


Christian Neukirchen

2/19/2006 10:36:00 AM

0

"Harold Hausman" <hhausman@gmail.com> writes:

> attr_* are written in c if I'm not mistaken. That might explain (at
> least some of) the difference.

Not only that, it directly circumvents further method calls and
accesses the instance variable table directly. No way to beat that in
pure Ruby.

> +1 on this being an excellent quiz. Thanks again Ara

Full ack.

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