tags:

views:

111

answers:

2

Hi, I'm adding a new method to the String class in Ruby, How can I get the value of the class?

e.g.:

class String
 def myMethodName(arguments)
   # here I want the string value
 end
end

puts "Lennie".myMethodName()
# this should return "Lennie" or whatever the value of the string is.

Thanks

+1  A: 

How about using "self"?

Considering you're adding the a method to String, self refers to the string itself. I suppose if you are not certain you could use "self to_s" to make certain you really get the string value of the surrounding object.

so:

def mymethod(args)
  self
end

should do the trick

DefLog
+3  A: 

You should be able to access the actual string value by referring to self:

class String
    def myMethodName()
        self
    end
end

puts "Lennie".myMethodName()
# => "Lennie"
Rudd Zwolinski