From: Andrea Dallera on
[Note: parts of this message were removed to make it a legal post.]

Have you tried it like this?


---

def initialize(attributes = nil)

super attributes

step = 0

national_debt = 0

sum_of_income = 0

sum_of_taxes = 0

sum_of_happiness = 0

sum_of_investments = 0

end

---


'self' is the default receiver inside a method

HTH

--
Andrea Dallera
http://github.com/bolthar/freightrain
http://usingimho.wordpress.com



Il 16/06/2010 11.31, Ralph Shnelvar ha scritto:
>
> Consider
>
>
> ---
>
> def initialize(attributes = nil)
>
> super attributes
>
> self.step = 0
>
> self.national_debt = 0
>
> self.sum_of_income = 0
>
> self.sum_of_taxes = 0
>
> self.sum_of_happiness = 0
>
> self.sum_of_investments = 0
>
> self
>
> end
>
> ---
>
>
>
> Please focus on the repeated use of self.
>
>
> Is there some way to set the, uh, environment so that "self" need not
> be repeated?
>
>
> (Joke: Hmm, if you get rid of your self, do you commit suicide?)
>
>
>
>


From: Jesús Gabriel y Galán on
On Wed, Jun 16, 2010 at 11:37 AM, Andrea Dallera
<andrea(a)andreadallera.com> wrote:
> Have you tried it like this?
>
>
> ---
>
> def initialize(attributes = nil)
>
>  super attributes
>
>  step                = 0
>
>  national_debt       = 0
>
>  sum_of_income       = 0
>
>  sum_of_taxes        = 0
>
>  sum_of_happiness    = 0
>
>  sum_of_investments  = 0
>
> end
>
> ---
>
>
> 'self' is the default receiver inside a method

That won't work, because the parser sees this as local variable
assignments, and not method calls.
I don't know an easy way to remove that.

Jesus.

From: Jason Roelofs on
2010/6/16 Jesús Gabriel y Galán <jgabrielygalan(a)gmail.com>

> On Wed, Jun 16, 2010 at 11:37 AM, Andrea Dallera
> <andrea(a)andreadallera.com> wrote:
> > Have you tried it like this?
> >
> >
> > ---
> >
> > def initialize(attributes = nil)
> >
> > super attributes
> >
> > step = 0
> >
> > national_debt = 0
> >
> > sum_of_income = 0
> >
> > sum_of_taxes = 0
> >
> > sum_of_happiness = 0
> >
> > sum_of_investments = 0
> >
> > end
> >
> > ---
> >
> >
> > 'self' is the default receiver inside a method
>
> That won't work, because the parser sees this as local variable
> assignments, and not method calls.
> I don't know an easy way to remove that.
>

If they're ivars (attr_accessible), then just use the ivar itself. If they
are methods on the class, then you'll need the self for there reason you
mentioned:

def initialize(attributes = nil)

super # If exact signature, don't need arguments

@step = 0

@national_debt = 0

@sum_of_income = 0

@sum_of_taxes = 0

@sum_of_happiness = 0

@sum_of_investments = 0
end

Oh, and you never need to return anything from #initialize.

Jason

From: Jesús Gabriel y Galán on
On Wed, Jun 16, 2010 at 1:28 PM, Jason Roelofs <jameskilton(a)gmail.com> wrote:
> 2010/6/16 Jesús Gabriel y Galán <jgabrielygalan(a)gmail.com>
>
>> On Wed, Jun 16, 2010 at 11:37 AM, Andrea Dallera
>> <andrea(a)andreadallera.com> wrote:
>> > Have you tried it like this?
>> >
>> >
>> > ---
>> >
>> > def initialize(attributes = nil)
>> >
>> >  super attributes
>> >
>> >  step                = 0
>> >
>> >  national_debt       = 0
>> >
>> >  sum_of_income       = 0
>> >
>> >  sum_of_taxes        = 0
>> >
>> >  sum_of_happiness    = 0
>> >
>> >  sum_of_investments  = 0
>> >
>> > end
>> >
>> > ---
>> >
>> >
>> > 'self' is the default receiver inside a method
>>
>> That won't work, because the parser sees this as local variable
>> assignments, and not method calls.
>> I don't know an easy way to remove that.
>>
>
> If they're ivars (attr_accessible), then just use the ivar itself. If they
> are methods on the class, then you'll need the self for there reason you
> mentioned:
>
> def initialize(attributes = nil)
>
>  super  # If exact signature, don't need arguments
>
>  @step                = 0
>
>  @national_debt       = 0
>
>  @sum_of_income       = 0
>
>  @sum_of_taxes        = 0
>
>  @sum_of_happiness    = 0
>
>  @sum_of_investments  = 0
> end

Only if you assume that the setter methods only set the ivar. If
there's extra logic in the setter method, to call it you need the
self.

Jesus.

From: Intransition on


On Jun 16, 5:31 am, Ralph Shnelvar <ral...(a)dos32.com> wrote:
> Consider
>
>
> def initialize(attributes = nil)
>
>
>
>   super attributes
>
>
>
>   self.step                = 0
>

step.=(0)

But do you really want to do that?

The setters don't bother me so much. And I actually like seeing the
self sometimes b/c it gets highlighted by my editor. Aside however, I
really hate having to type 'self.class', I'd much prefer Ruby add an
alias #object_class along the lines of #object_id.


 |  Next  |  Last
Pages: 1 2 3 4
Prev: unsubscribe
Next: Compare two objects