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.
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2006-05-15 08: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).
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-15 10:16
(Received via mailing list)
On May 15, 2006, at 2:44 AM, Giles Bowkett 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-05-15 13:50
(Received via mailing list)
2006/5/15, Giles Bowkett <gilesb@gmail.com>:
>   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
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-05-15 14:45
(Received via mailing list)
On 5/15/06, Logan Capaldo <logancapaldo@gmail.com> 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.
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 Mauricio Fernandez (Guest)
on 2006-05-15 15:25
(Received via mailing list)
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>
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-05-15 15:37
(Received via mailing list)
On 5/15/06, Mauricio Fernandez <mfp@acm.org> 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!
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2006-05-15 20: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()?
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-05-15 20:45
(Received via mailing list)
On 5/15/06, Giles Bowkett <gilesb@gmail.com> 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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-15 22:23
(Received via mailing list)
On May 15, 2006, at 8:43 AM, 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.
>
> --
> R. Mark Volkmann
> 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.
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2006-05-15 22:45
(Received via mailing list)
On 5/15/06, Austin Ziegler <halostatue@gmail.com> wrote:
> On 5/15/06, Giles Bowkett <gilesb@gmail.com> 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

?
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-05-15 22:55
(Received via mailing list)
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:
> > > 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.