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).

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

attr :channel, true
@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

2006/5/15, Giles B. [email protected]:

attr :channel, true
@channel = channel
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).

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

On 5/15/06, Logan C. [email protected] wrote:

attr :note_length, true
@track = Track.new(sequence)

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.

On Mon, May 15, 2006 at 09:43:20PM +0900, Mark V. 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) # => #

On 5/15/06, Mauricio F. [email protected] wrote:

RUBY_VERSION # => “1.8.4”
Sample.new(2, 3, 4, 5) # => #

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

On 5/15/06, Giles B. [email protected] wrote:

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

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 May 15, 2006, at 8:43 AM, Mark V. 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.


R. Mark V.
Object Computing, Inc.

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 5/15/06, Austin Z. [email protected] wrote:

On 5/15/06, Giles B. [email protected] wrote:

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…

[“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, Giles B. [email protected] wrote:

On 5/15/06, Austin Z. [email protected] wrote:

On 5/15/06, Giles B. [email protected] wrote:

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