Forum: Ruby Struct.new vs class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Giles B. (Guest)
on 2006-05-15 10:44
(Received via mailing list)
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).
Logan C. (Guest)
on 2006-05-15 12:16
(Received via mailing list)
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
Robert K. (Guest)
on 2006-05-15 15:50
(Received via mailing list)
2006/5/15, Giles B. <removed_email_address@domain.invalid>:
>   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
Mark V. (Guest)
on 2006-05-15 16:45
(Received via mailing list)
On 5/15/06, Logan C. <removed_email_address@domain.invalid> 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.
Mauricio F. (Guest)
on 2006-05-15 17:25
(Received via mailing list)
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)   # => #<struct Sample note_length=2, sequence=3,
note=4, channel=5>
Mark V. (Guest)
on 2006-05-15 17:37
(Received via mailing list)
On 5/15/06, Mauricio F. <removed_email_address@domain.invalid> wrote:
>
> 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!
Giles B. (Guest)
on 2006-05-15 22:38
(Received via mailing list)
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()?
Austin Z. (Guest)
on 2006-05-15 22:45
(Received via mailing list)
On 5/15/06, Giles B. <removed_email_address@domain.invalid> 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
Logan C. (Guest)
on 2006-05-16 00:23
(Received via mailing list)
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.
Giles B. (Guest)
on 2006-05-16 00:45
(Received via mailing list)
On 5/15/06, Austin Z. <removed_email_address@domain.invalid> wrote:
> On 5/15/06, Giles B. <removed_email_address@domain.invalid> 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

?
Austin Z. (Guest)
on 2006-05-16 00:55
(Received via mailing list)
On 5/15/06, Giles B. <removed_email_address@domain.invalid> wrote:
> On 5/15/06, Austin Z. <removed_email_address@domain.invalid> wrote:
> > On 5/15/06, Giles B. <removed_email_address@domain.invalid> 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
This topic is locked and can not be replied to.