implicit parameters

N

neleai

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
 
Z

zycte

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
 
T

ts

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%
 
N

neleai

ts said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top