Until recently, I didn't think this was possible in Ruby short of using an anonymous hash and parsing the hash in the 'initialize' method. However, Guy Decoux recently provided this idiom:
module SomeMod
def initialize(attributes)
attributes.each do |k, v|
type.send(:attr_accessor, k)
send("#{k}=", v)
end
end
end
sm = SomeMod.new(name=>'Dan',rank=>'SrA')
puts sm.rank #=> 'SrA'
sm.rank = 'General'
puts sm.rank #=> 'General'
Not only does this let you do named arguments with Ruby, it automatically provides get/set ability. Will Perl 6 be able to do this, hmmm?
The only downside is that you'll have to manually parse out any bogus attributes a programmer might try and create.
Type checking (Score:2)
Re:Type checking (Score:2)
I'm genuinely curious as to what advantage you think this would bring to Ruby, or whether or not you think this is a real selling point for Python.
Re:Type checking (Score:3, Insightful)
I meant simply that this sort of thing (named params) should be built in, with checking done on the actual names passed in, performed at the parser level. I don't mean to imply checking of the type of a variable.
This covers 90% of the times you need to pass named parameters (the other 10% being where you might not know the names you need to pass ahead of time - in which case you construct your API so you can pass a hash instead), and would
Re:Type checking (Score:2)
Will Perl 6 handle this any differently?