to make dot method dot method work?

P

Pen Ttt

here is the class
class Mycompute
def initialize(str)
@str=str
end
def values
@@result=@str
end
def up
@@result.upcase
end
end
irb(main):012:0> Mycompute.new("Abc").values
=> "Abc"
irb(main):013:0>
irb(main):014:0* Mycompute.new("Abc").up
=> "ABC"
irb(main):015:0> Mycompute.new("Abc").values.up
NoMethodError: undefined method `up' for "Abc":String
from (irb):15
from :0

would you tell me how to fix it to make
Mycompute.new("Abc").values.up output "ABC"?
 
P

Pavel Varela

i'm totally newbie in ruby, sorry if im talking about smth obvious, just
trying to help, but do u know u code doesn't work if it's like this ?

class Mycompute
def initialize(str)
@str=str
end
def values
@@result=@str
end
def up
@@result.upcase
end
end

puts Mycompute.new("Abc").up

but it do if u do this:

class Mycompute
def initialize(str)
@str=str
@@result=str
end
...

puts Mycompute.new("Abc").up #will work.
 
R

Rob Biedenharn

The most obvious way I see to do it is to use an object attribute
instead of a class attribute. in other words, @result instead of
@@result. Is there a reason why you want to use a class attribute in
this instance? A class attribute is not an attribute of the object.


You've defined #up on Mycompute instances, but not on string. If
Mycompute#values returned a Mycompute instance, then you could write
the expression that way. Since #values seems to want to be a plain
String, what about turning the expression around:

Mycompute.new("Abc").up.values

(and I do find it odd to use "values" rather than "value" since only
one thing is returned)

class Mycompute
def initialize(str)
@str = str
end
def up
self.class.new(@str.upcase)
end
def values
@str
end
def to_s
@str.dup
end
end

irb> Mycompute.new("Abc")
=> #<Mycompute:0x63cb8 @str="Abc">
irb> Mycompute.new("Abc").up
=> #<Mycompute:0x60d74 @str="ABC">
irb> Mycompute.new("Abc").values
=> "Abc"
irb> Mycompute.new("Abc").up.values
=> "ABC"

irb> puts Mycompute.new("Abc")
Abc
=> nil
irb> puts Mycompute.new("Abc").up
ABC
=> nil
irb> puts Mycompute.new("Abc").values
Abc
=> nil
irb> puts Mycompute.new("Abc").up.values
ABC
=> nil

Does that help you?

-Rob

Rob Biedenharn
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
B

Brian Candler

When you call foo.bar.baz then you are:
1. calling method 'foo'
2. that returns an object
3. calling 'bar' on the object returned from (2)
4. that returns an object
5. calling 'baz' on the object returned from (4)

So:
class Mycompute
def initialize(str)
@str=str
end
def values
@@result=@str
end
def up
@@result.upcase
end
end
irb(main):012:0> Mycompute.new("Abc").values
=> "Abc"
irb(main):013:0>
irb(main):014:0* Mycompute.new("Abc").up
=> "ABC"

That returns a string (and sets @@result as a side-effect).
irb(main):015:0> Mycompute.new("Abc").values.up
NoMethodError: undefined method `up' for "Abc":String
from (irb):15
from :0

Strings don't have a method called 'up' - but they do have 'upcase'.

So you can do:

Mycompute.new("Abc").values.upcase

Or else perhaps this is what you want:

obj = Mycompute.new("Abc")
obj.values
obj.up

Or else:

def values
@@result = @str
self
end

in which case Mycompute.new("Abc").values.up will work, because your
values method returns the object itself, not a String.
 
P

Pen Ttt

thinks for everyone,i learned :
1.it's not necessary to use @@ in the code
2.to use self to get an object
here is the code what i want
class Mycompute
def initialize(str)
@str=str
end
def values
@result=@str
self
end
def up
@[email protected]
end
end

Mycompute.new("Abc").values.up
=> "ABC"
there is another problem ,
Mycompute.new("Abc").values+"d"
NoMethodError: undefined method `+' for #<Mycompute:0xb74ad8d4
@str="Abc", @result="Abc">
how to make Mycompute.new("Abc").values+"d" output "Abcd"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top