Initializer with self

I have tho classes A and B1, and b is defined as:

a = A.new
b = a.foo # b is an instance of class B1

I would like to define b directly:
b = B2.new

where the class B2 is defined:
class B2
def initialize
a = A.new
self = a.foo # <<< error!
end

end

But i cannot define self in the constructor…

How can i solve this problem.

Thanks on advance,

iwan

Hi,

This makes no sense. Even if you could redefine “self”, it’s certainly a
bad idea to change the way objects are created. Why would you want an
object “mascerading” as a completely different object?

So you’ll have to stick with the class method (though I have no idea
what this is for).

On Aug 8, 2012, at 3:32 AM, Iwan B. wrote:

def initialize
a = A.new
self = a.foo # <<< error!
end

end

But i cannot define self in the constructor…

You cannot re-define self, it is defined for you to be the current
instance of class B2. Why do you need it? Even if it were possible,
don’t you find it weird - you create and instance of B2 with B2.new, but
get back the instance of B1?

Gennady.

This is the ‘real’ case (used for .xlsx files generation, via axlsx
gem):
p = Axlsx::Package.new
wb = p.workbook
wb.styles.add_style sz: 16, b: true, alignment: { horizontal: :center }
wb.add_worksheet(:name => “Basic Worksheet”) do |sheet|

I don’t need the package object ‘p’, except for get a workbook. But i
want to create a custom worksheet class, so i can define my predefined
styles:
Inside initialise method:
@my_styles = {}
@my_styles[:heading] = styles.add_style sz: 16, b: true, alignment: {
horizontal: :center }

def apply_style(name)
@my_styles[name.to_sym]
end

Remark that ‘styles’ is a workbook method.
Finally:
wb = MyWorkbook.new

sheet[“B1”].style = wb.apply_style :boo

Thank you!

On Wed, Aug 8, 2012 at 12:54 PM, Jan E. [email protected] wrote:

This makes no sense. Even if you could redefine “self”, it’s certainly a
bad idea to change the way objects are created.

And especially instance “a” would be lost unless instances of class B1
have a reference back to an instance of A. Basically instance of A in
this scenario is completely superfluous. If B1’s can only be created
via A.new then a method like this would be the way to go

def create(…)
A.new(…).foo
end

Kind regards

On Wed, Aug 8, 2012 at 2:05 PM, Iwan B. [email protected] wrote:

This is the ‘real’ case (used for .xlsx files generation, via axlsx
gem):
p = Axlsx::Package.new
wb = p.workbook

Well, you could do

class MyPackage < ::Axlsx::Package
class MyWorkbook < ::Axlsx::Workbook
def initialize(*a, &b)
super
# add styles
end
end

def workbook
MyWorkbook.new(self)
end
end

Or, if you just want to add default styles and values you could do

def ::Axlsx::Workbook.my_workbook
workbook.tap do |wb|
wb.styles.add_style sz: 16, b: true, alignment: { horizontal:
:center }

end
end

wb.styles.add_style sz: 16, b: true, alignment: { horizontal: :center }
wb.add_worksheet(:name => “Basic Worksheet”) do |sheet|

I don’t need the package object ‘p’, except for get a workbook.

It may be stored inside the workbook.

Cheers

robert

Nice

Thank you Robert!

Iwan B. wrote in post #1071643:

class B2
def initialize
a = A.new
self = a.foo # <<< error!
end

end

It is possible to override the ‘new’ method:

class B2
def self.new
return A.new
end
end

But this is very likely to confuse people who call B2.new and expect to
get an instance of B2.

If you want to choose the class dynamically then the normal approach is
a factory method:

class B2
def self.create(*args)
klass = pick_a_class # choose your own logic here
klass.new(*args)
end
end

But it seems to me that subclassing is closest to what you’re trying to
achieve (making something which is like a workbook, but slightly
modified)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs