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?
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
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.
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
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>
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
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?