class Point
def +(other) #there your code, i think
result = Point.new
result.x = self.x + other.x
result.y = self.y + other.y
result.z = self.z + other.z
return result
end
end
but you also need to make the variables accessable like
class Point
attr_accessor :x,:y,:z
end
That last line is IMPORTANT. Remember, Ruby methods treat the last value
evaluated in a method as the return value. So if you don't put self in
then the return value for the + method will be the last addition.
So now with the above:
c = a + b
is the equivalent of doing:
c = a.+(b)
HTH.
···
On 10/04/2011 01:11 AM, Thescholar Thescholar wrote:
--
Darryl L. Pierce <mcpierce@gmail.com> http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
class Point
def +(other) #there your code, i think
result = Point.new
result.x = self.x + other.x
result.y = self.y + other.y
result.z = self.z + other.z
return result
end
end
but you also need to make the variables accessable like
class Point
attr_accessor :x,:y,:z
end
And add '@' to them in the constructor.
···
On 10/04/2011 01:23 AM, Hans Mackowiak wrote:
and you should remove your init, it is wrong
--
Darryl L. Pierce <mcpierce@gmail.com> http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
Warning!! That will *mutate* the point passed as the left-hand argument
of the '+'!
I would suggest instead:
def +(other)
Point.new(x+other.x, y+other.y, z+other.z)
end
DOH! Good point!
(Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you
think it's clearer)
I don't think so. 'x' is a local variable, while '@x' is an instance
variable. And since the fields are instance variables they have to be
prefixed with '@'.
···
On 10/04/2011 10:03 AM, Brian Candler wrote:
--
Darryl L. Pierce <mcpierce@gmail.com> http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
Not quite. This is one of Ruby's little gotchas. A naked "x" will be
taken as a local variable in the method. If you want to use an
instance variable you *must* precede it with either "self." or "@".
-Dave
···
On Tue, Oct 4, 2011 at 10:03, Brian Candler <b.candler@pobox.com> wrote:
(Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you
think it's clearer)
--
LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web site.
Main Web Site: davearonson.com
Programming Blog: codosaur.us
Excellence Blog: dare2xl.com
If there's no local variable in the scope, the interpreter sees it as a
method call. If it's ambiguous, it'll pick the variable over the method
call, unless you force it with a trailing ().
x = 1
def x; 2; end
puts x
puts x()
···
On Tue, Oct 4, 2011 at 3:53 PM, Darryl L. Pierce <mcpierce@gmail.com> wrote:
> (Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you
> think it's clearer)
I don't think so. 'x' is a local variable, while '@x' is an instance
variable. And since the fields are instance variables they have to be
prefixed with '@'.
"x = exp" will _always_ assign to a local variable. If you want to
invoke the setter method you _must_ do "self.x = expr".
2. reading
"x" will access a local variable if it is defined and fall back to the
getter method if there is no variable. "self.x" will always invoke
the getter method regardless whether there is a local variable or not.
Needless to say that it's not the best of practices to mix local
variables and properties of the same name.
Kind regards
robert
···
On Tue, Oct 4, 2011 at 5:52 PM, Dave Aronson <rubytalk2dave@davearonson.com> wrote:
On Tue, Oct 4, 2011 at 10:03, Brian Candler <b.candler@pobox.com> wrote:
(Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you
think it's clearer)
Not quite. This is one of Ruby's little gotchas. A naked "x" will be
taken as a local variable in the method. If you want to use an
instance variable you *must* precede it with either "self." or "@".
Sorry, Brian was right. Try it out in the context of the code sample he
posted to see for yourself.
···
On 10/04/2011 11:52 AM, Dave Aronson wrote:
On Tue, Oct 4, 2011 at 10:03, Brian Candler <b.candler@pobox.com> wrote:
(Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you
think it's clearer)
Not quite. This is one of Ruby's little gotchas. A naked "x" will be
taken as a local variable in the method. If you want to use an
instance variable you *must* precede it with either "self." or "@".
--
Darryl L. Pierce <mcpierce@gmail.com> http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
Okay, then this flies in the face of how I understood variables. I would
have thought the act of having 'x' in the definition for bar in the
above created a local variable in the current scope.
···
On 10/04/2011 11:11 AM, Adam Prescott wrote:
(Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you
think it's clearer)
I don't think so. 'x' is a local variable, while '@x' is an instance
variable. And since the fields are instance variables they have to be
prefixed with '@'.
If there's no local variable in the scope, the interpreter sees it as a
method call. If it's ambiguous, it'll pick the variable over the method
call, unless you force it with a trailing ().
x = 1
def x; 2; end
puts x
puts x()
--
Darryl L. Pierce <mcpierce@gmail.com> http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
"x" will access a local variable if it is defined and fall back to the
getter method if there is no variable. "self.x" will always invoke
the getter method regardless whether there is a local variable or not.
x() too, as an alternative to self.x. Although I prefer the latter.
Needless to say that it's not the best of practices to mix local
variables and properties of the same name.
+1!
···
On Tue, Oct 4, 2011 at 4:58 PM, Robert Klemme <shortcutter@googlemail.com>wrote:
My understanding (probly flawed) is
attr_reader :x
generates the following code:
def x @x
end
so in bar that 'x' is a call to the x method, which returns the '@x' value
but doing it this way makes the x public readable, so should only be
used if that is needed
I'd stick with using @x
Only an assignment creates a local variable in the scope.
def bar
x = 2
end
This creates a local variable x in the method bar. If there are no
assignments, the parser understands that references to x must be
method calls. This is due to the syntax ambiguity between methods and
local variables, because ruby allows you to call methods without
parens. So, to break the ambiguity, the parser marks references as
methods by default, and only as local variables when it sees an
assignment.
Jesus.
···
On Tue, Oct 4, 2011 at 5:31 PM, Darryl L. Pierce <mcpierce@gmail.com> wrote:
On 10/04/2011 11:11 AM, Adam Prescott wrote:
(Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you
think it's clearer)
I don't think so. 'x' is a local variable, while '@x' is an instance
variable. And since the fields are instance variables they have to be
prefixed with '@'.
If there's no local variable in the scope, the interpreter sees it as a
method call. If it's ambiguous, it'll pick the variable over the method
call, unless you force it with a trailing ().
x = 1
def x; 2; end
puts x
puts x()
Okay, then this flies in the face of how I understood variables. I would
have thought the act of having 'x' in the definition for bar in the
above created a local variable in the current scope.
This is actually only 1/7th true. There are as many as 7 ways to
introduce a new variable, out of which anybody would guess about 5 if
they though about it for a while ;), and 2 are quite obscure.
These are:
1. Single- or multiple-assignment (including sub-assignment) anywhere
an expression is allowed
2. A for loop's iterator variable(s)
3. Formal arguments to a method
4. Block arguments and block-local variables
5. Stabby Lambda variables
6. Rescue exception naming (rescue StandardError => err)
7. Named Regexp captures in literal matches (/name: (?<person>\w+)/ =~
'name: Mike' creates a local variable named "person" with value
"Mike")
AH! That's what I missing, and what he meant by "the method". Okay. The
OP though didn't have the attr_accessor on the fields, though, that I
remember.
···
On 10/04/2011 11:42 AM, Chris Hulan wrote:
My understanding (probly flawed) is
attr_reader :x
generates the following code:
def x @x
end
so in bar that 'x' is a call to the x method, which returns the '@x' value
but doing it this way makes the x public readable, so should only be
used if that is needed
I'd stick with using @x
--
Darryl L. Pierce <mcpierce@gmail.com> http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
Only an assignment creates a local variable in the scope.
This is actually only 1/7th true. There are as many as 7 ways to
introduce a new variable, out of which anybody would guess about 5 if
they though about it for a while ;), and 2 are quite obscure.
These are:
1. Single- or multiple-assignment (including sub-assignment) anywhere
an expression is allowed
2. A for loop's iterator variable(s)
Kind of assignment
3. Formal arguments to a method
Good one, I was thinking only in the body of the method, but you can
also argue that someone does an assignment to them
4. Block arguments and block-local variables
Block arguments and block local variables are local to the block
scope, not the method.