I sometimes find myself doing something like this:
class A
def initialize(a, b, c) @a = a @b = b @c = c
end
end
Of course there would be more to the class, but that initialize method
seems a little redundant. Wouldn’t it be neater to be able to do “def
initialize(@a, @b, @c);end” and be done with it? I tried to do that on a
whim, and got something like “formal argument cannot be an instance
variable”.
I’m sort of a perpetual nuby here, so I’d like to hear why this isn’t
possible. I have a feeling that the alternative I propose makes no sense
on some theoretical level that is beyond my feeble grasp. I guess it is
because they are “formal arguments”, so it wouldn’t make sense to have
them persist over the lifetime of the object the way instance variables
normally do.
Well, to follow up to myself, I would have to say that wouldn’t make a
whole lot of sense. initialize would have to be some kind of “special”
method to act like that, much more so than it is now.
I’ve solved my “problem”, such as it was, by writing a little Vim script
(using Ruby!) that, when called on a line like “A b c”, builds a
class A
def initialize(b, c) @b = b @c = c
end
end
-type skeleton out of it. It was fun, too.
Wouldn’t it be neater to be able to do “def initialize(@a, @b, @c);end” and be done with it?
Well, to follow up to myself, I would have to say that wouldn’t make a
whole lot of sense. initialize would have to be some kind of “special”
method to act like that, much more so than it is now.
I’ve solved my “problem”, such as it was, by writing a little Vim script
(using Ruby!) that, when called on a line like “A b c”, builds a
class A
def initialize(b, c) @b = b @c = c
end
end
-type skeleton out of it. It was fun, too.
Wouldn’t it be neater to be able to do “def initialize(@a, @b, @c);end” and be done with it?
Well, to follow up to myself, I would have to say that wouldn’t make a
whole lot of sense. initialize would have to be some kind of “special”
method to act like that, much more so than it is now.
I’ve solved my “problem”, such as it was, by writing a little Vim script
(using Ruby!) that, when called on a line like “A b c”, builds a
class A
def initialize(b, c) @b = b @c = c
end
end
-type skeleton out of it. It was fun, too.
Another fun little Ruby-ism is that you could use “Struct” as a shortcut
for constructing the initializer as in the following example:
Wouldn’t it be neater to be able to do “def initialize(@a, @b, @c);end” and be done with it?
Well, to follow up to myself, I would have to say that wouldn’t make a
whole lot of sense. initialize would have to be some kind of “special”
method to act like that, much more so than it is now.
I’ve solved my “problem”, such as it was, by writing a little Vim script
(using Ruby!) that, when called on a line like “A b c”, builds a
class A
def initialize(b, c) @b = b @c = c
end
end
-type skeleton out of it. It was fun, too.
# Define initialize in terms of the symbols contained in *syms.
# e.g. autoinit :a, :b =>
# def initialize(a, b)
# @a = a
# @b = b
# end
def autoinit(*syms)
vars = syms.map { |s| s.to_s }
params = vars.join ","
body = vars.map { |v| "@#{v} = #{v}"}.join "; "
code = <<-EOF
def initialize(#{params})
#{body}
end
EOF
module_eval code
end
end
class Example
autoinit :a, :b, :c
end
p Example.new(1,2,3)
And then check out www.ruby-talk.org/11663, where I’ve just realised
Avi has done the same thing but better.
Gavin
···
On Monday, January 13, 2003, 12:11:34 AM, Gavin wrote:
Now, even though it’s untested, I’m pretty sure it wouldn’t work, cos
I left a hole for you to fill (i.e. I started it and it became too
hard)
That’s pretty slick. I had not thought to do that, but now that
I see it, it makes perfect sense. module/class_eval is something I ran
across a while back, but kind of forgot about because it didn’t seem
like I’d ever use it. Thanks for posting that.
That’s pretty slick. I had not thought to do that, but now that
I see it, it makes perfect sense. module/class_eval is something I ran
across a while back, but kind of forgot about because it didn’t seem
like I’d ever use it. Thanks for posting that.