From: Daniel Finnie on
I think the standard idiom is to use the min/max functions of an array:

a = 5
b = 10
[a, b].max #=> 10

You can also give max a block, similar to sort:

a = "Hello"
b = "Hi"
[a, b].max {|x, y| x.length <=> y.length}

Or you can write a method so it works more like sort_by (the interface,
not the implementation):

class Array
def max_by &blk
max {|a, b| blk.call(a) <=> blk.call(b)}
end
end

a = "Hello"
b = "Hi"
[a, b].max {|x| x.length}

And then, in Ruby 1.9, you should be able to do this (using max_by from
above):

a = "Hello"
b = "Hi"
[a, b].max(&:length) # Not sure if the syntax is 100%

And if you still want your max() function:

def min(*args)
args.min
end

Everything also applies to minimums using the min function.

Dan

Tom Ayerst wrote:
> Sorry for my beginners ruby (are there some standard min(x,y)/max(x,y,)
> functions?

From: William James on

William James wrote:
> Simon Kröger wrote:
> > Dear Ruby Quiz,
> >
> > this isn't really a solution to the quiz 109 because it violates
> > some (if not all) of the rules. But as James noted there was a
> > code golf problem very similar to this quiz and here is my
> > solution to that.
> > (see http://codegolf.com/oblongular-number-spirals for detailed
> > description of the code golf problem)
> >
> > ----------------------------------------------------------------
> > s,f=1,proc{|x,y|y<1?[]:[[*s...s+=x]]+f[y-1,x].reverse.transpose}
> > puts f[w=gets(' ').to_i,gets.to_i].map{|i|['%3i']*w*' '%i}
> > ----------------------------------------------------------------
> >
> > It draws a number spiral, starting with '1' in the upper left
> > corner and the highest number in the middle, it also features
> > spirals that are not quadratic.
> >
> > Yes, you will get some score at the codegolf site if you repost
> > this solution there - but nowadays you will only get to Rank 9
> > with this solution and of course you will start to feel ill and
> > you won't be able to sleep for days and other nasty things might
> > happen if you do so.
> >
> > If someone can derive an even shorter solution from this i would
> > be very interested to see it (the best ruby solution today has 7
> > bytes less)
> >
> > cheers
> >
> > Simon
>
> I can't get this to work.
>
> E:\Ruby>ruby try.rb
> 4 4
> try.rb:2:in `%': too few arguments. (ArgumentError)
> from try.rb:2
> from try.rb:2:in `map'
> from try.rb:2

irb(main):006:0> s=1; x=5; [*s...s+x]
=> [1, 2, 3, 4, 5]
irb(main):007:0> s=1; x=5; [*s...s+=x]
=> []
irb(main):009:0> s=1; x=5; s...s+=x
=> 6...6

From: Simon Kröger on

>> I can't get this to work.

Hmm.

>> E:\Ruby>ruby try.rb
>> 4 4
>> try.rb:2:in `%': too few arguments. (ArgumentError)
>> from try.rb:2
>> from try.rb:2:in `map'
>> from try.rb:2
>
> irb(main):006:0> s=1; x=5; [*s...s+x]
> => [1, 2, 3, 4, 5]
> irb(main):007:0> s=1; x=5; [*s...s+=x]
> => []
> irb(main):009:0> s=1; x=5; s...s+=x
> => 6...6

Interesting:

C:\development>ruby -v -e "s=1; x=5; p s...s+=x"
ruby 1.8.5 (2006-08-25) [i386-mswin32]
1...6


cheers

Simon
From: William James on
Simon Kröger wrote:
> >> I can't get this to work.
>
> Hmm.
>
> >> E:\Ruby>ruby try.rb
> >> 4 4
> >> try.rb:2:in `%': too few arguments. (ArgumentError)
> >> from try.rb:2
> >> from try.rb:2:in `map'
> >> from try.rb:2
> >
> > irb(main):006:0> s=1; x=5; [*s...s+x]
> > => [1, 2, 3, 4, 5]
> > irb(main):007:0> s=1; x=5; [*s...s+=x]
> > => []
> > irb(main):009:0> s=1; x=5; s...s+=x
> > => 6...6
>
> Interesting:
>
> C:\development>ruby -v -e "s=1; x=5; p s...s+=x"
> ruby 1.8.5 (2006-08-25) [i386-mswin32]
> 1...6
>
>
> cheers
>
> Simon

E:\Ruby>ruby -v
ruby 1.8.2 (2004-12-25) [i386-mswin32]

So it seems that this won't work without modification under 1.8.2.

As for making it shorter,
change
puts f[w=gets(' ').to_i,gets.to_i].map{|i|['%3i']*w*' '%i}
to
puts f[w=gets.to_i,$_[-3,2].to_i].map{|i|"%3d "*w%i}

An invisible extra space is printed at the end of each line.

From: William James on
Simon Kröger wrote:
> Dear Ruby Quiz,
>
> this isn't really a solution to the quiz 109 because it violates
> some (if not all) of the rules. But as James noted there was a
> code golf problem very similar to this quiz and here is my
> solution to that.
> (see http://codegolf.com/oblongular-number-spirals for detailed
> description of the code golf problem)
>
> ----------------------------------------------------------------
> s,f=1,proc{|x,y|y<1?[]:[[*s...s+=x]]+f[y-1,x].reverse.transpose}
> puts f[w=gets(' ').to_i,gets.to_i].map{|i|['%3i']*w*' '%i}
> ----------------------------------------------------------------
>
> It draws a number spiral, starting with '1' in the upper left
> corner and the highest number in the middle, it also features
> spirals that are not quadratic.
>
> Yes, you will get some score at the codegolf site if you repost
> this solution there - but nowadays you will only get to Rank 9
> with this solution and of course you will start to feel ill and
> you won't be able to sleep for days and other nasty things might
> happen if you do so.
>
> If someone can derive an even shorter solution from this i would
> be very interested to see it (the best ruby solution today has 7
> bytes less)

A reduction:

["stuff"]+[[4,5,6],[:x,:y,:z]].reverse.transpose
==>["stuff", [:x, 4], [:y, 5], [:z, 6]]
["stuff"]+(a,b=[[4,5,6],[:x,:y,:z]];b.zip a)
==>["stuff", [:x, 4], [:y, 5], [:z, 6]]

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6
Prev: Send over raw socket?
Next: Web service client help needed