Class or not?

Hi
what are the differences
    between

class Point < Struct.new(:x, :y);
  ...
end

    and

Point = Struct.new(:x, :y) {
  ...
}

Thanks
Berg

1 - If you want to extend the Struct class you use:
  class Point < Struct
  ...
  end
2 - If you want to create an object from the class Struct, you use:
  point = Struct.new(:x, :y)

···

Em Seg, 2016-07-04 às 03:09 +0200, A Berger escreveu:

Hi
what are the differences
between

class Point < Struct.new(:x, :y);
...
end
and
Point = Struct.new(:x, :y) {
...
}
Thanks
Berg
Unsubscribe: ruby-talk-request@ruby-lang.org?subject=unsubscr
>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi Berger,

At the second example, you're passing a block to #new method.
The #new method will evaluate the block at some point.
Look at the Rubinius code for Struct:

klass.module_eval(&block) if block

Then it will return an unamed class.
If you assign it to a constant, the class grabs its name from it.
You will be able to call #new on this constant as normal.

As it's a normal method invocation with a block, inside the block
you have access to the surround context.

outer_var = "Anything"

Point = Struct.new(:x, :y) do
  puts outer_var
end

# It Works

class Point < Struct.new(:x, :y)
  puts outer_var
end

# It raises an error.

Abinoam Jr.

···

On Sun, Jul 3, 2016 at 10:09 PM, A Berger <aberger7890@gmail.com> wrote:

Hi
what are the differences
    between

class Point < Struct.new(:x, :y);
  ...
end

    and

Point = Struct.new(:x, :y) {
  ...
}

Thanks
Berg

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

You get one or two new classes.

robert

···

On Mon, Jul 4, 2016 at 3:09 AM, A Berger <aberger7890@gmail.com> wrote:

what are the differences
    between

class Point < Struct.new(:x, :y);
  ...
end

    and

Point = Struct.new(:x, :y) {
  ...
}

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Since Struct.new returns a subclass of Struct anyway
you always should use the second form.

Directly from the docs for Struct:

"This is the recommended way to customize a struct. Subclassing an
anonymous struct creates an extra anonymous class that will never
be used."

BTW, please try to use better subject lines, they are very vague;
something like "How to use Struct" or "Subclassing Struct or not"
are easier to spot for prospective repliers.

Regards,
Marcus

···

Am 04.07.2016 um 03:09 schrieb A Berger:

what are the differences between

class Point < Struct.new(:x, :y);
  ...
end

    and

Point = Struct.new(:x, :y) {
  ...
}

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Illustrated:

2.3.1 (main):0 > class Point < Struct.new(:x, :y); end
=> nil
2.3.1 (main):0 > Spoint = Struct.new(:x, :y) {}
=> Spoint < Struct
2.3.1 (main):0 > Point.ancestors
=> [
  [0] Point < #<Class:0x007fa68c4bcf28>,
  [1] #<Class:0x007fa68c4bcf28> < Struct,
  [2] Struct < Object,
  [3] Enumerable,
  [4] Object < BasicObject,
  [5] PP::ObjectMixin,
  [6] Kernel,
  [7] BasicObject
]
2.3.1 (main):0 > Spoint.ancestors
=> [
  [0] Spoint < Struct,
  [1] Struct < Object,
  [2] Enumerable,
  [3] Object < BasicObject,
  [4] PP::ObjectMixin,
  [5] Kernel,
  [6] BasicObject
]

···

On Mon, Jul 4, 2016 at 1:05 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

You get one or two new classes.

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan
Consulting Availability : Silicon Valley or remote