The Artima Developer Community
Sponsored Link

Java Buzz Forum
create_many_arrays :foo, :bar, :baz

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
create_many_arrays :foo, :bar, :baz Posted: Jun 15, 2006 9:47 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: create_many_arrays :foo, :bar, :baz
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement

A bunch of lazy programmers that I know looked at some code that had a lot of:

@foo = []
@bar = []
@baz = []

Instead they wanted to do:

create_many_arrays :foo, :bar, :baz

NOTE: watching someone do "foo = bar = baz = []" is fun :)

So:

def create_many_arrays(*arraynames)
  params = arraynames.join ','
  arrays = ''
  attrs = ''

arraynames.each do |name|
thename = name.to_s
arrays += "@#{thename} = []\n"
attrs += "attr_accessor :#{name}\n"
end

code = <<-END_OF_CODE
alias :__dion__initialize :initialize
#{attrs}

def initialize
__dion__initialize
#{arrays}
end

def initialize(*incoming)
__dion__initialize *incoming
#{arrays}
end
END_OF_CODE

class_eval code
end


class Foo
create_many_arrays :foo, :bar
end

f = Foo.new
f.foo
f.foo << 1
f.foo

class Bar
attr_accessor :a

def initialize(a)
@a = a
end

create_many_arrays :foo, :bar
end

After a broken impl like this, you then realise that there is no real reason to want to do this, and your own code is broken if you want data structures like this.

Read: create_many_arrays :foo, :bar, :baz

Topic: Eclipse3.2 Splash Screen Final Release Previous Topic   Next Topic Topic: Flickr: Nagios: System and Network Monitoring by Wolfgang Barth

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use