"methods" method

Hi

I tried String.methods-Kernel.methods in irb, found that most methods of
String are inherited from Kernel. I don’t know for methods like sub, gsub,
split, strip, chomp… Are they String specific? Why they exist in Kernel?

Thanks!
Shannon

···

STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail

I tried String.methods-Kernel.methods in irb, found that most methods of
String are inherited from Kernel. I don't know for methods like sub, gsub,

No, you have found that it exist method with the same name in Kernel and
String. These methods do the same thing but don't operate on the same
receiver.

split, strip, chomp... Are they String specific? Why they exist in Kernel?

ruby is not an abstraction, these methods for example are usefull to write
one-liners

Guy Decoux

Hi

I tried String.methods-Kernel.methods in irb, found that most methods of
String are inherited from Kernel. I don’t know for methods like sub, gsub,
split, strip, chomp… Are they String specific? Why they exist in Kernel?

Thanks!
Shannon

Kernel::X often translates to
$.X ($ is a String) or
$<.X or $>.X ($< and $> are IO)

So, instead of
while line = STDIN.gets
STDOUT.puts line.chomp
end

you can get all Perly and write
while gets
puts chomp
end

Nah, stuff that! Go
puts chomp while gets

See? Examine the Kernel module in Pickaxe/ri for more details.

Gavin

···

From: “Shannon Fang” xrfang@hotmail.com

Hi

I tried String.methods-Kernel.methods in irb, found that most methods of
String are inherited from Kernel. I don’t know for methods like sub, gsub,
split, strip, chomp… Are they String specific? Why they exist in Kernel?

Thanks!
Shannon
Hi Shannon,

I try ti answer with my poor english :wink:

kernet is a module include in the super class “Object”, all the methods
of this module are usable in any point of your code.

String is a predefined class of the module kernel and is ancestor is the
super class “Object”: it is an instance of the class “Object”.

This is the raison that you can use something like the predefined
variable $_ like this:

irb(main):014:0> $_ =“Hello, world!\n”
“Hello, world!\n”
irb(main):015:0> print $_
Hello, world!
nil
irb(main):016:0> $_ = chop + " nice dayto play with ruby ;)"
“Hello, world! nice dayto play with ruby ;)”
irb(main):017:0> chop
“Hello, world! nice dayto play with ruby ;”
irb(main):018:0>

Daniel

···

Le jeu 05/12/2002 à 11:54, Shannon Fang a écrit :


STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail

Hi,

···

At Thu, 5 Dec 2002 19:54:50 +0900, Shannon Fang wrote:

I tried String.methods-Kernel.methods in irb, found that most methods
of String are inherited from Kernel. I don’t know for methods like
sub, gsub, split, strip, chomp… Are they String specific? Why they
exist in Kernel?

Note that String.methods returns singleton methods of String.

I imagine what you really want is:
String.instance_methods-Kernel.instance_methods


Nobu Nakada

Hm, you should try `String.instance_methods-Kernel.instance_methods’
then immediately remember classes have two distinct kind of methods :slight_smile:

···

In message F143y8VBDUfdfdhNzW30000b85e@hotmail.com xrfang@hotmail.com writes:

I tried String.methods-Kernel.methods in irb, found that most methods of
String are inherited from Kernel. I don’t know for methods like sub, gsub,
split, strip, chomp… Are they String specific? Why they exist in Kernel?


kjana@dm4lab.to December 5, 2002
Translators, traitors.