Struct.new vs class

OK, so I have this class and it could probably be a Struct.

class Sample

  # I should probably turn this whole class into a simple
Struct.new(etc.) statement

  attr :note_length, true
  attr :track, true
  attr :note, true
  attr :channel, true
  attr :threshold, true
  attr :notes, true

  def initialize(note_length, sequence, note, channel, threshold, notes)
    @note_length = note_length

    @track = Track.new(sequence)
    @track.sequence.tracks << @track # butt

    @note = note
    @channel = channel
    @threshold = threshold
    @notes = notes
  end

end

Please forgive my laziness, because I'm certain this is in Pickaxe,
Why's Poignant Guide, Ruby In A Nutshell, "The Ruby Way," and a dozen
other resources as well, most of which are either a bookshelf away or
a Google away, but if anybody wants to just humor me, what's the best
way to simplify this heinous, Java-esque code? Can I throw it in a
Struct and then just extend initialize without losing any of the stuff
Struct provides?

The line with the comment "butt" is unavoidable (for now).

···

--
Giles Bowkett
http://www.gilesgoatboy.org

OK, so I have this class and it could probably be a Struct.

class Sample

# I should probably turn this whole class into a simple
Struct.new(etc.) statement

attr :note_length, true
attr :track, true
attr :note, true
attr :channel, true
attr :threshold, true
attr :notes, true

def initialize(note_length, sequence, note, channel, threshold, notes)
   @note_length = note_length

   @track = Track.new(sequence)
   @track.sequence.tracks << @track # butt

   @note = note
   @channel = channel
   @threshold = threshold
   @notes = notes
end

end

class Sample < Struct.new(:note_length, :sequence, :note, :channel, etc.)
    def initialize
      super
      # blah
    end
end

···

On May 15, 2006, at 2:44 AM, Giles Bowkett wrote:

Please forgive my laziness, because I'm certain this is in Pickaxe,
Why's Poignant Guide, Ruby In A Nutshell, "The Ruby Way," and a dozen
other resources as well, most of which are either a bookshelf away or
a Google away, but if anybody wants to just humor me, what's the best
way to simplify this heinous, Java-esque code? Can I throw it in a
Struct and then just extend initialize without losing any of the stuff
Struct provides?

The line with the comment "butt" is unavoidable (for now).

--
Giles Bowkett
http://www.gilesgoatboy.org

# untested
S = Struct.new :note_length, :sequence, :note, :channel, :threshold,
:notes, :track
class S
  alias :_initialize :initialize
  def initialize(note_length, sequence, note, channel, threshold, notes, track)
    _initialize(note_length, sequence, note, channel, threshold,
notes, Track.new)
    self.track.sequence.tracks << track
  end
end

Kind regards

robert

···

2006/5/15, Giles Bowkett <gilesb@gmail.com>:

OK, so I have this class and it could probably be a Struct.

class Sample

  # I should probably turn this whole class into a simple
Struct.new(etc.) statement

  attr :note_length, true
  attr :track, true
  attr :note, true
  attr :channel, true
  attr :threshold, true
  attr :notes, true

  def initialize(note_length, sequence, note, channel, threshold, notes)
    @note_length = note_length

    @track = Track.new(sequence)
    @track.sequence.tracks << @track # butt

    @note = note
    @channel = channel
    @threshold = threshold
    @notes = notes
  end

end

Please forgive my laziness, because I'm certain this is in Pickaxe,
Why's Poignant Guide, Ruby In A Nutshell, "The Ruby Way," and a dozen
other resources as well, most of which are either a bookshelf away or
a Google away, but if anybody wants to just humor me, what's the best
way to simplify this heinous, Java-esque code? Can I throw it in a
Struct and then just extend initialize without losing any of the stuff
Struct provides?

The line with the comment "butt" is unavoidable (for now).

--
Have a look: Robert K. | Flickr

What is this supposed to do? It doesn't seem to allow me to create a
Sample object and pass parameters to new. I think the OP wanted that.

···

On 5/15/06, Logan Capaldo <logancapaldo@gmail.com> wrote:

On May 15, 2006, at 2:44 AM, Giles Bowkett wrote:

> OK, so I have this class and it could probably be a Struct.
>
> class Sample
>
> # I should probably turn this whole class into a simple
> Struct.new(etc.) statement
>
> attr :note_length, true
> attr :track, true
> attr :note, true
> attr :channel, true
> attr :threshold, true
> attr :notes, true
>
> def initialize(note_length, sequence, note, channel, threshold,
> notes)
> @note_length = note_length
>
> @track = Track.new(sequence)
> @track.sequence.tracks << @track # butt
>
> @note = note
> @channel = channel
> @threshold = threshold
> @notes = notes
> end
>
> end
>

class Sample < Struct.new(:note_length, :sequence, :note, :channel,
etc.)
    def initialize
      super
      # blah
    end
end

--
R. Mark Volkmann
Object Computing, Inc.

class Sample < Struct.new(:note_length, :sequence, :note, :channel)
    def initialize(*a)
      super
      # ...
    end
end

RUBY_VERSION # => "1.8.4"
Sample.new(2, 3, 4, 5) # => #<struct Sample note_length=2, sequence=3, note=4, channel=5>

···

On Mon, May 15, 2006 at 09:43:20PM +0900, Mark Volkmann wrote:

What is this supposed to do? It doesn't seem to allow me to create a
Sample object and pass parameters to new. I think the OP wanted that.

--
Mauricio Fernandez - http://eigenclass.org - singular Ruby

oops sorry. Whenever I answer questions like this, half the time I think the questioner is asking to be reminded of the idiom (because that's what usually happens to me), and I tend to leave stuff out.

···

On May 15, 2006, at 8:43 AM, Mark Volkmann wrote:

On 5/15/06, Logan Capaldo <logancapaldo@gmail.com> wrote:

On May 15, 2006, at 2:44 AM, Giles Bowkett wrote:

> OK, so I have this class and it could probably be a Struct.
>
> class Sample
>
> # I should probably turn this whole class into a simple
> Struct.new(etc.) statement
>
> attr :note_length, true
> attr :track, true
> attr :note, true
> attr :channel, true
> attr :threshold, true
> attr :notes, true
>
> def initialize(note_length, sequence, note, channel, threshold,
> notes)
> @note_length = note_length
>
> @track = Track.new(sequence)
> @track.sequence.tracks << @track # butt
>
> @note = note
> @channel = channel
> @threshold = threshold
> @notes = notes
> end
>
> end
>

class Sample < Struct.new(:note_length, :sequence, :note, :channel,
etc.)
    def initialize
      super
      # blah
    end
end

What is this supposed to do? It doesn't seem to allow me to create a
Sample object and pass parameters to new. I think the OP wanted that.

-- R. Mark Volkmann
Object Computing, Inc.

Ah! Your initialize method takes a "*a" parameter, but the original
example did not. That fixes it. Thanks!

···

On 5/15/06, Mauricio Fernandez <mfp@acm.org> wrote:

On Mon, May 15, 2006 at 09:43:20PM +0900, Mark Volkmann wrote:
> What is this supposed to do? It doesn't seem to allow me to create a
> Sample object and pass parameters to new. I think the OP wanted that.

class Sample < Struct.new(:note_length, :sequence, :note, :channel)
    def initialize(*a)
      super
      # ...
    end
end

RUBY_VERSION # => "1.8.4"
Sample.new(2, 3, 4, 5) # => #<struct Sample note_length=2, sequence=3, note=4, channel=5>

--
R. Mark Volkmann
Object Computing, Inc.

is "*a" basically a keyword args flag that tells initialize() to take
any and all args and just pass them up the chain to super()?

···

On 5/15/06, Mark Volkmann <r.mark.volkmann@gmail.com> wrote:

On 5/15/06, Mauricio Fernandez <mfp@acm.org> wrote:
> On Mon, May 15, 2006 at 09:43:20PM +0900, Mark Volkmann wrote:
> > What is this supposed to do? It doesn't seem to allow me to create a
> > Sample object and pass parameters to new. I think the OP wanted that.
>
> class Sample < Struct.new(:note_length, :sequence, :note, :channel)
> def initialize(*a)
> super
> # ...
> end
> end
>
> RUBY_VERSION # => "1.8.4"
> Sample.new(2, 3, 4, 5) # => #<struct Sample note_length=2, sequence=3, note=4, channel=5>

Ah! Your initialize method takes a "*a" parameter, but the original
example did not. That fixes it. Thanks!

--
R. Mark Volkmann
Object Computing, Inc.

--
Giles Bowkett
http://www.gilesgoatboy.org

is "*a" basically a keyword args flag that tells initialize() to take
any and all args and just pass them up the chain to super()?

Except that it isn't keyword args. I like David Black's name for it:
the (un)array operator. It's an unary non-overridable operator on
arrays. When used on an array as a receiver, it indicates that it will
take a list of items and treat them as an array; when used on a sender
array, it splits that array out into a list:

  >> a = %w(a b c d e)
  => ["a", "b", "c", "d", "e"]
  >> b, c, *d = *a
  => ["a", "b", "c", "d", "e"]
  >> p a, b, c, d
  ["a", "b", "c", "d", "e"] # a
  "a" # b
  "b" # c
  ["c", "d", "e"] # d

-austin

···

On 5/15/06, Giles Bowkett <gilesb@gmail.com> wrote:
--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

> is "*a" basically a keyword args flag that tells initialize() to take
> any and all args and just pass them up the chain to super()?

Except that it isn't keyword args. I like David Black's name for it:
the (un)array operator.

I might have been thinking of a Python term...

It's an unary non-overridable operator on
arrays. When used on an array as a receiver, it indicates that it will
take a list of items and treat them as an array; when used on a sender
array, it splits that array out into a list:

  >> a = %w(a b c d e)
  => ["a", "b", "c", "d", "e"]
  >> b, c, *d = *a
  => ["a", "b", "c", "d", "e"]
  >> p a, b, c, d
  ["a", "b", "c", "d", "e"] # a
  "a" # b
  "b" # c
  ["c", "d", "e"] # d

so when you feed it to initialize() like that, it translates the list
it's been given into an array and then initialize() picks that up and
says "oh, ok, this is the sequence of args I'm expecting"?

and you could also do

def initialize (*arbitrarily_long_name)
end

?

···

On 5/15/06, Austin Ziegler <halostatue@gmail.com> wrote:

On 5/15/06, Giles Bowkett <gilesb@gmail.com> wrote:

--
Giles Bowkett
http://www.gilesgoatboy.org

> > is "*a" basically a keyword args flag that tells initialize() to take
> > any and all args and just pass them up the chain to super()?
> Except that it isn't keyword args. I like David Black's name for it:
> the (un)array operator.
I might have been thinking of a Python term...

You might have been. I believe that Python supports both *args and
**args, but I could be wrong. (**args is the keyword args flag.)

so when you feed it to initialize() like that, it translates the list
it's been given into an array and then initialize() picks that up and
says "oh, ok, this is the sequence of args I'm expecting"?

and you could also do

def initialize (*arbitrarily_long_name)
end

Close enough.

-austin

···

On 5/15/06, Giles Bowkett <gilesb@gmail.com> wrote:

On 5/15/06, Austin Ziegler <halostatue@gmail.com> wrote:
> On 5/15/06, Giles Bowkett <gilesb@gmail.com> wrote:

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca