Instance vars as arguments to initialize()?

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.

···

In article slrnb202t7.l1k.invalid@mojo.chicken.com, Jim Bob wrote:

Wouldn’t it be neater to be able to do “def initialize(@a, @b,
@c);end” and be done with it?

Hi –

···

On Sun, 12 Jan 2003, Jim Bob wrote:

In article slrnb202t7.l1k.invalid@mojo.chicken.com, Jim Bob wrote:

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.

Just for historical interest, you might want to have a look at
the thread beginning at http://www.ruby-talk.org/11633.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Jim Bob wrote:

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:

====BEGIN====
Blah = Struct.new(“Blah”, :a, :b, :c)

class Blah
def extra_method
puts “Here’s some non-instance-var behaviour”
end
end

x = Blah.new(“Hello World”,Math::PI,[“an”, “array”, “of”, “strings”])
x.extra_method #=> prints “Here’s some non-instance-var behaviour”
puts x.a #=> prints “Hello World”
====END====

Chad

···

In article slrnb202t7.l1k.invalid@mojo.chicken.com, Jim Bob wrote:

You could try this too (untested):

class Class
def autoinit(*args)
# Define “initialize” in terms of the symbols contained in *args
end
end

class Example
autoinit :a, :b, :c
end

Now, even though it’s untested, I’m pretty sure it wouldn’t work, cos
I left a hole for you to fill :slight_smile: (i.e. I started it and it became too
hard)

Gavin

···

On Sunday, January 12, 2003, 7:51:41 AM, Jim wrote:

In article slrnb202t7.l1k.invalid@mojo.chicken.com, Jim Bob wrote:

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.

OK, I’ve done it. Run this:

class Class

 # 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 :slight_smile: (i.e. I started it and it became too
hard)

Very nice. I didn’t even know the Struct class existed. Got to crack the
books more often!

···

In article 3E20FCF6.8030405@chadfowler.com, Chad Fowler wrote:

Another fun little Ruby-ism is that you could use “Struct” as a shortcut
for constructing the initializer as in the following example:

====BEGIN====
Blah = Struct.new(“Blah”, :a, :b, :c)

class Blah
def extra_method
puts “Here’s some non-instance-var behaviour”
end
end

x = Blah.new(“Hello World”,Math::PI,[“an”, “array”, “of”, “strings”])
x.extra_method #=> prints “Here’s some non-instance-var behaviour”
puts x.a #=> prints “Hello World”
====END====

Chad

In article Pine.LNX.4.44.0301111740160.4623-100000@candle.superlink.net,
dblack@candle.superlink.net wrote:

Just for historical interest, you might want to have a look at
the thread beginning at http://www.ruby-talk.org/11633.

David

Thank you for the link. I had looked on groups.google.com, but didn’t
find that thread. I’ve got a new bookmark now.

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.

···

In article 114440202848.20030113020723@soyabean.com.au, Gavin Sinclair wrote:

OK, I’ve done it. Run this:

Well that was the first time I ever used it; just wanted to get it out
of my system.

Gavin

···

On Monday, January 13, 2003, 5:29:43 AM, Jim wrote:

In article 114440202848.20030113020723@soyabean.com.au, Gavin Sinclair wrote:

OK, I’ve done it. Run this:

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.