[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

fun with "case"

Robert Klemme

6/10/2007 10:33:00 AM


A funny (and readable) way to test collection sizes just occurred to me:

irb(main):001:0> class Integer
irb(main):002:1> def elements
irb(main):003:2> cond = lambda {|enum| self == enum.size}
irb(main):004:2> class <<cond
irb(main):005:3> alias :=== :call
irb(main):006:3> end
irb(main):007:2> cond
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> case [1,2,3]
irb(main):011:1> when 3.elements
irb(main):012:1> puts "three!"
irb(main):013:1> when 5.elements
irb(main):014:1> puts "too much!"
irb(main):015:1> else
irb(main):016:1* puts "else"
irb(main):017:1> end
three!
=> nil

:-)

Kind regards

robert
15 Answers

Joachim Glauche

6/10/2007 10:50:00 AM

0

Why not do a simply use

case [1,2,3].size

instead?

--
Posted via http://www.ruby-....

Robert Klemme

6/10/2007 11:17:00 AM

0

On 10.06.2007 12:49, Joachim Glauche wrote:
> Why not do a simply use
>
> case [1,2,3].size
>
> instead?

Because a) it's too simple and bloody obvious, b) less fun (see subject
:-)) and c) it does not work if you also have other criteria (i.e. which
do not use the size but content). :-)

Kind regards

robert

Bertram Scharpf

6/10/2007 11:32:00 AM

0

Hi,

Am Sonntag, 10. Jun 2007, 19:49:43 +0900 schrieb Joachim Glauche:
> Why not do a simply use
>
> case [1,2,3].size
>
> instead?

Maybe you want to test for other properties.

Besides that it's a lot of fun finding out what one can do
in a sohisticated programming language.

Therefore.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Gustav - Railist

6/10/2007 11:44:00 AM

0

Robert Klemme wrote:
>
> A funny (and readable) way to test collection sizes just occurred to me:
>
> irb(main):001:0> class Integer
> irb(main):002:1> def elements
> irb(main):003:2> cond = lambda {|enum| self == enum.size}
> irb(main):004:2> class <<cond
> irb(main):005:3> alias :=== :call
> irb(main):006:3> end
> irb(main):007:2> cond
> irb(main):008:2> end
> irb(main):009:1> end
> => nil
> irb(main):010:0> case [1,2,3]
> irb(main):011:1> when 3.elements
> irb(main):012:1> puts "three!"
> irb(main):013:1> when 5.elements
> irb(main):014:1> puts "too much!"
> irb(main):015:1> else
> irb(main):016:1* puts "else"
> irb(main):017:1> end
> three!
> => nil
>
> :-)
>
> Kind regards
>
> robert
>
>
lol, that's pretty sweet :-)

G

Robert Dober

6/10/2007 11:50:00 AM

0

On 6/10/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 10.06.2007 12:49, Joachim Glauche wrote:
> > Why not do a simply use
> >
> > case [1,2,3].size
> >
> > instead?
>
> Because a) it's too simple and bloody obvious, b) less fun (see subject
> :-)) and c) it does not work if you also have other criteria (i.e. which
> do not use the size but content). :-)
or in other words you can write

case list
when []
puts :empty
when 1
puts :not_empty
else
puts :close_to_infinity
end

as a matter of fact writing

case list.size
becomes an unnecessary early commitment!!!

T'is really kool Robert

Cheers
Robert
>
> Kind regards
>
> robert
>
>


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Robert Dober

6/10/2007 12:05:00 PM

0

On 6/10/07, Robert Klemme <shortcutter@googlemail.com> wrote:
>
> A funny (and readable) way to test collection sizes just occurred to me:
>
> irb(main):001:0> class Integer
> irb(main):002:1> def elements
> irb(main):003:2> cond = lambda {|enum| self == enum.size}
> irb(main):004:2> class <<cond
> irb(main):005:3> alias :=== :call
> irb(main):006:3> end
> irb(main):007:2> cond
> irb(main):008:2> end
> irb(main):009:1> end
> => nil
> irb(main):010:0> case [1,2,3]
> irb(main):011:1> when 3.elements
> irb(main):012:1> puts "three!"
> irb(main):013:1> when 5.elements
> irb(main):014:1> puts "too much!"
> irb(main):015:1> else
> irb(main):016:1* puts "else"
> irb(main):017:1> end
> three!
> => nil
>
> :-)
>
> Kind regards
>
> robert
>
>
As I said, this is really cool, now here comes a first quick hack of
generalization, you gotta file an RCR for this ;)
Please note the absence of "@" in my code ;)


class Module
def define_casey args={}
arg_mth = args[:on]
name = args[:name]
trans = args[:transform]
define_method name do
cond = lambda{ |x|
trans ? self.send(trans) == x.send( arg_mth ) :
self == x.send( arg_mth )
}
class << cond
alias_method :===, :call
end
cond
end
end
end

class Integer
define_casey :on => :size, :name => :elements
end

case []
when 0.elements
puts :empty
end

What you think?

Cheers
Robert


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Robert Dober

6/10/2007 12:16:00 PM

0

Sorry forgot the best ;)

class Object
def identity; self end # I wanted this for a long time, maybe itself
would be a good name too
end
class Module
def define_casey opts={}
arg_mth = opts[:on]
name = opts[:name]
trans = opts[:transform]
define_method name do
| *args |
cond = lambda{ |x|
trans ? self.send(trans, *args) == x.send( arg_mth ) :
self == x.send( arg_mth )
}
class << cond
alias_method :===, :call
end
cond
end
end
end

class Integer
define_casey :on => :size, :name => :elements
define_casey :on => :identity, :name => :plus, :transform => :+
end

case []
when 0.elements
puts :empty
end

case 42
when 41.plus( 1 )
puts "The number"
end


Robert

Robert Klemme

6/10/2007 6:53:00 PM

0

On 10.06.2007 14:15, Robert Dober wrote:
> Sorry forgot the best ;)
>
> class Object
> def identity; self end # I wanted this for a long time, maybe itself
> would be a good name too
> end
> class Module
> def define_casey opts={}
> arg_mth = opts[:on]
> name = opts[:name]
> trans = opts[:transform]
> define_method name do
> | *args |
> cond = lambda{ |x|
> trans ? self.send(trans, *args) == x.send( arg_mth ) :
> self == x.send( arg_mth )
> }
> class << cond
> alias_method :===, :call
> end
> cond
> end
> end
> end
>
> class Integer
> define_casey :on => :size, :name => :elements
> define_casey :on => :identity, :name => :plus, :transform => :+
> end
>
> case []
> when 0.elements
> puts :empty
> end
>
> case 42
> when 41.plus( 1 )
> puts "The number"
> end
>
>
> Robert

I am not sure whether utility of define_casey is fully clear to me yet -
so I leave it to you to write the RCR. :-)

But I do think you're getting the hang of Ruby. :-)

Kind regards

robert

Robert Dober

6/10/2007 7:06:00 PM

0

On 6/10/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 10.06.2007 14:15, Robert Dober wrote:

>
> I am not sure whether utility of define_casey is fully clear to me yet -
> so I leave it to you to write the RCR. :-)
No it is your code and idea, I just let ruby write it;)
No more RCRs I have already got mine, would not be fair to others;)
Seriously now, I did not write define_casey because I wanted to show
some code, I believe that your idea might be very good for some more
readable code; I am desperately looking for applications now.
>
> But I do think you're getting the hang of Ruby. :-)
Time to change to Io, just kidding.
Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Marc Heiler

6/10/2007 7:28:00 PM

0

If its fun, its nice :-)
In ruby i also enjoy when its short and terse (if i manage to understand
it) though :-)

--
Posted via http://www.ruby-....