[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

implicit parameters

neleai@seznam.cz

6/20/2006 10:17:00 AM

Hi,
I am trying to write program that allows you add arbitrary default
value for functions ie:
def blit(source,target,x,y,wi=source.width,he=source.heigth)
I use folowing script which adds foo=expr if !foo to source but it is
bit dirty solution.
Is there any way how incorporate this in ruby?
modif=""
IO.read(ARGV[0]).each{|line|
if line=~ /(def[^(]*)\((.*)\)/
line+=$1+'('
ad=""
$2.split(?;).each{|w| #if I use colon I must use ruby
parser for spliting
if w=~ /(.*)=(.*)/ #
line+=$1+?,
ad+="#{w} if !#{$1}\n"
else
modif+=w
end
modif.chop!+=ad
}
else
modif+=line
end
}
puts modif

4 Answers

zycte

6/20/2006 11:25:00 AM

0

If you mean what I think you mean, it's already there...

irb(main):015:0> def tt(a, b=5)
irb(main):016:1> puts a
irb(main):017:1> puts b
irb(main):018:1> end
=> nil
irb(main):019:0> tt(1)
1
5
=> nil
irb(main):020:0> tt(1,2)
1
2
=> nil

On 2006-06-20 12:16:31 +0200, neleai@seznam.cz said:

> Hi,
> I am trying to write program that allows you add arbitrary default
> value for functions ie:
> def blit(source,target,x,y,wi=source.width,he=source.heigth)
> I use folowing script which adds foo=expr if !foo to source but it is
> bit dirty solution.
> Is there any way how incorporate this in ruby?
> modif=""
> IO.read(ARGV[0]).each{|line|
> if line=~ /(def[^(]*)\((.*)\)/
> line+=$1+'('
> ad=""
> $2.split(?;).each{|w| #if I use colon I must use ruby
> parser for spliting
> if w=~ /(.*)=(.*)/ #
> line+=$1+?,
> ad+="#{w} if !#{$1}\n"
> else
> modif+=w
> end
> modif.chop!+=ad
> }
> else
> modif+=line
> end }
> puts modif


neleai@seznam.cz

6/21/2006 2:49:00 PM

0


zycte wrote:
> If you mean what I think you mean, it's already there...
>
> irb(main):015:0> def tt(a, b=5)
Isnt. I mean use like def tt(a,b=sin(a))

ts

6/21/2006 2:56:00 PM

0

>>>>> "n" == neleai <neleai@seznam.cz> writes:

n> Isnt. I mean use like def tt(a,b=sin(a))

Try this

moulon% ruby -e 'include Math; def x(a, b = sin(a)) p a, b; end; x(12)'
12
-0.536572918000435
moulon%

or

moulon% cat b.rb
#!/usr/bin/ruby
class A
attr_reader :x, :y

def initialize(x, y)
@x, @y = x, y
end
end

def x(a, b = a.x, c = a.y)
p a, b, c
end

x(A.new(12,24))

moulon%

moulon% ./b.rb
#<A:0xb7d559b4 @y=24, @x=12>
12
24
moulon%


--

Guy Decoux

neleai@seznam.cz

6/22/2006 5:03:00 AM

0


ts wrote:
> >>>>> "n" == neleai <neleai@seznam.cz> writes:
>
> n> Isnt. I mean use like def tt(a,b=sin(a))
>
> Try this
>
> moulon% ruby -e 'include Math; def x(a, b = sin(a)) p a, b; end; x(12)'
> 12
> -0.536572918000435

Another feature I didnt know.
If only
def hard_programable_function() petrovitch end
work