7stud2
(7stud --)
9 March 2013 14:07
1
I was trying to learn the operator over-ridding in Ruby. So i used the
below in my IRB.
class Banner < String
def +
?> upcase
end
def -
?> downcase
end
end
=> nil
ban = Banner.new("hi")
=> "hi"
+ban
NoMethodError: undefined method `+@' for "hi":Banner
from (irb):22
from C:/Ruby193/bin/irb:12:in `<main>'
Now after seeing the error in the `IRB` i corrected definition of `+`
and `-` as below, which in turn fixed the code.
class Banner < String
def +@
upcase
end
def -@
downcase
end
end
=> nil
ban = Banner.new("hi")
=> "hi"
+ban
=> "HI"
But my confusion is with the logical necessity of that `@` operator?
Again I tried to over-ride `!` as below:
class Banner
def !
reverse
end
end
=> nil
!ban
=> "ih"
not ban
=> "ih"
But here I didn't need to use that `@` operator.
Can anyone help me to understand why `@` was needed with `+` and `-` but
not with `!`?
Thanks
···
--
Posted via http://www.ruby-forum.com/\ .
+ and - can be both unary and binary operators, so there are differently named methods for the different operations. Ruby’s Unary Operators and How to Redefine Their Functionality might be intresting to you .
Hope this helps,
Mike
···
On 2013-03-09, at 9:07 AM, "Kumar R." <lists@ruby-forum.com> wrote:
I was trying to learn the operator over-ridding in Ruby. So i used the
below in my IRB.
class Banner < String
def +
?> upcase
end
def -
?> downcase
end
end
=> nil
ban = Banner.new("hi")
=> "hi"
+ban
NoMethodError: undefined method `+@' for "hi":Banner
from (irb):22
from C:/Ruby193/bin/irb:12:in `<main>'
Now after seeing the error in the `IRB` i corrected definition of `+`
and `-` as below, which in turn fixed the code.
class Banner < String
def +@
upcase
end
def -@
downcase
end
end
=> nil
ban = Banner.new("hi")
=> "hi"
+ban
=> "HI"
But my confusion is with the logical necessity of that `@` operator?
Again I tried to over-ride `!` as below:
class Banner
def !
reverse
end
end
=> nil
!ban
=> "ih"
not ban
=> "ih"
But here I didn't need to use that `@` operator.
Can anyone help me to understand why `@` was needed with `+` and `-` but
not with `!`?
Thanks
--
Posted via http://www.ruby-forum.com/\ .
--
Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.
7stud2
(7stud --)
10 March 2013 20:27
3
But my confusion is with the logical necessity of that `@` operator?
···
--
Posted via http://www.ruby-forum.com/ .
7stud2
(7stud --)
10 March 2013 20:34
4
there is no @ operator, its part of the method name TO DIFFERATE between
obj + obj2 and +obj
! does not have a method for obj ! obj2 so it does not need an @ at the
unary name
same for methods that end with !, they only use that for this method
names when there is a non-! method too like gsub has a gsub! partner,
but replace is not called replace! because there is not an second method
···
--
Posted via http://www.ruby-forum.com/ .
Inheriting from String is a bad idea. Really.
Cheers
robert
···
On Sat, Mar 9, 2013 at 3:07 PM, Kumar R. <lists@ruby-forum.com> wrote:
I was trying to learn the operator over-ridding in Ruby. So i used the
below in my IRB.
class Banner < String
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/