[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: adding anotehr each method to String

Chris Shea

3/25/2007 7:41:00 AM

On Mar 25, 2:20 am, unbewusst.s...@wortanschahung.com.invalid (Une
Bévue) wrote:
> f it doesn't exist allready, i'd like to add another each method to
> String similar to each_byte except it loops over chars for example :
>
> "zero".each_char{|c| puts c}
>
> would print-out :
> z
> e
> r
> o
>
> i know how to add a new method to a given class, what i don't know is
> specifically for methods like each which pass an arg to a given block...
>
> --
> It's easy to play any musical instrument: all you have to do is
> touch the right key at the right time and the instrument will
> play itself. -- J.S. Bach

What you need is "yield".

This does what you need:

class String
def each_char
0.upto(self.length - 1) do |x|
yield self[x,1]
end
end
end

yield passes a value to a block.

8 Answers

unbewusst.sein

3/25/2007 9:23:00 AM

0

Chris Shea <cmshea@gmail.com> wrote:

>
> What you need is "yield".
>
> This does what you need:
>
> class String
> def each_char
> 0.upto(self.length - 1) do |x|
> yield self[x,1]
> end
> end
> end
>
> yield passes a value to a block.

Ok, fine thanks a lot !

does the method :
String.to_a doing :

s="a string"
sa=s.to_a
#=> ['a',' ','s','t','r','i','n','g']

exists allready ?

generally speaking how to list-out, for a given class all the methods ?

in order to avoid collapsing them.
--
It's easy to play any musical instrument: all you have to do is
touch the right key at the right time and the instrument will
play itself. -- J.S. Bach

Harold Hausman

3/25/2007 9:29:00 AM

0

On 3/25/07, Une Bévue <unbewusst.sein@wortanschahung.com.invalid> wrote:
> Ok, fine thanks a lot !
>
> does the method :
> String.to_a doing :
>
> s="a string"
> sa=s.to_a
> #=> ['a',' ','s','t','r','i','n','g']
>
> exists allready ?
>

irb(main):001:0> "a string".split( "" )
=> ["a", " ", "s", "t", "r", "i", "n", "g"]

> generally speaking how to list-out, for a given class all the methods ?
>

irb(main):002:0> "a string".methods.sort
=> ["%", "*", "+", "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", "[]", "
[]=", "__id__", "__send__", "all?", "any?", "between?", "capitalize", "capitaliz
e!", "casecmp", "center", "chomp", "chomp!", "chop", "chop!", "class", "clone",
"collect", "concat", "count", "crypt", "delete", "delete!", "detect", "display",
"downcase", "downcase!", "dump", "dup", "each", "each_byte", "each_line", "each
_with_index", "empty?", "entries", "eql?", "equal?", "extend", "find", "find_all
", "freeze", "frozen?", "gem", "grep", "gsub", "gsub!", "hash", "hex", "id", "in
clude?", "index", "inject", "insert", "inspect", "instance_eval", "instance_of?"
, "instance_variable_defined?", "instance_variable_get", "instance_variable_set"
, "instance_variables", "intern", "is_a?", "is_binary_data?", "is_complex_yaml?"
, "kind_of?", "length", "ljust", "lstrip", "lstrip!", "map", "match", "max", "me
mber?", "method", "methods", "min", "next", "next!", "nil?", "object_id", "oct",
"partition", "private_methods", "protected_methods", "public_methods", "reject"
, "replace", "require", "require_gem", "respond_to?", "reverse", "reverse!", "ri
ndex", "rjust", "rstrip", "rstrip!", "scan", "select", "send", "singleton_method
s", "size", "slice", "slice!", "sort", "sort_by", "split", "squeeze", "squeeze!"
, "strip", "strip!", "sub", "sub!", "succ", "succ!", "sum", "swapcase", "swapcas
e!", "taguri", "taguri=", "taint", "tainted?", "to_a", "to_f", "to_i", "to_s", "
to_str", "to_sym", "to_yaml", "to_yaml_properties", "to_yaml_style", "tr", "tr!"
, "tr_s", "tr_s!", "type", "unpack", "untaint", "upcase", "upcase!", "upto", "zi
p"]

;)

Hope that helps,
-Harold

unbewusst.sein

3/25/2007 10:46:00 AM

0

Une Bévue <unbewusst.sein@wortanschahung.com.invalid> wrote:

>
> exists allready ?

Yes, following :

ms=String.methods
puts ms.include?('to_a')


however :
s='toto'
s.to_a
# => ['toto']


--
It's easy to play any musical instrument: all you have to do is
touch the right key at the right time and the instrument will
play itself. -- J.S. Bach

unbewusst.sein

3/25/2007 10:59:00 AM

0

Harold Hausman <hhausman@gmail.com> wrote:

>
> irb(main):001:0> "a string".split( "" )
> => ["a", " ", "s", "t", "r", "i", "n", "g"]

OK fine, then no need to extend String )))
>
> > generally speaking how to list-out, for a given class all the methods ?
> >
>
> irb(main):002:0> "a string".methods.sort => ["%", "*", "+", "<", "<<",
> "<=", "<=>", "==", "===", "=~", ">", ">=", "[]", " []=", "__id__",
> "__send__", "all?", "any?", "between?", "capitalize", "capitaliz e!",
> "casecmp", "center", "chomp", "chomp!", "chop", "chop!", "class", "clone",
> "collect", "concat", "count", "crypt", "delete", "delete!", "detect",
> "display", "downcase", "downcase!", "dump", "dup", "each", "each_byte",
> "each_line", "each _with_index", "empty?", "entries", "eql?", "equal?",
> "extend", "find", "find_all ", "freeze", "frozen?", "gem", "grep", "gsub",
> "gsub!", "hash", "hex", "id", "in clude?", "index", "inject", "insert",
> "inspect", "instance_eval", "instance_of?" , "instance_variable_defined?",
> "instance_variable_get", "instance_variable_set" , "instance_variables",
> "intern", "is_a?", "is_binary_data?", "is_complex_yaml?" , "kind_of?",
> "length", "ljust", "lstrip", "lstrip!", "map", "match", "max", "me mber?",
> "method", "methods", "min", "next", "next!", "nil?", "object_id", "oct",
> "partition", "private_methods", "protected_methods", "public_methods",
> "reject" , "replace", "require", "require_gem", "respond_to?", "reverse",
> "reverse!", "ri ndex", "rjust", "rstrip", "rstrip!", "scan", "select",
> "send", "singleton_method s", "size", "slice", "slice!", "sort",
> "sort_by", "split", "squeeze", "squeeze!" , "strip", "strip!", "sub",
> "sub!", "succ", "succ!", "sum", "swapcase", "swapcas e!", "taguri",
> "taguri=", "taint", "tainted?", "to_a", "to_f", "to_i", "to_s", " to_str",
> "to_sym", "to_yaml", "to_yaml_properties", "to_yaml_style", "tr", "tr!" ,
> "tr_s", "tr_s!", "type", "unpack", "untaint", "upcase", "upcase!", "upto",
> "zi
> p"]
>
> ;)
>
> Hope that helps,

Yes a lot, thanks again !
--
It's easy to play any musical instrument: all you have to do is
touch the right key at the right time and the instrument will
play itself. -- J.S. Bach

Trans

3/25/2007 11:04:00 AM

0



On Mar 25, 6:00 am, unbewusst.s...@wortanschahung.com.invalid (Une
Bévue) wrote:
> Harold Hausman <hhaus...@gmail.com> wrote:
>
> > irb(main):001:0> "a string".split( "" )
> > => ["a", " ", "s", "t", "r", "i", "n", "g"]

"a string".split(//)

T.


Rick DeNatale

3/26/2007 10:53:00 PM

0

On 3/25/07, Une Bévue <unbewusst.sein@wortanschahung.com.invalid> wrote:
> Une Bévue <unbewusst.sein@wortanschahung.com.invalid> wrote:
>
> >
> > exists allready ?
>
> Yes, following :
>
> ms=String.methods
> puts ms.include?('to_a')
>
>
> however :
> s='toto'
> s.to_a
> # => ['toto']

This is because in Ruby a String doesn't act as a collection of
characters, it acts as a collection of lines.

s = "to\nto"
s.to_a => ["to\n", "to"]

"to\nto".each {|l| puts l}
to
to

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Gavin Kistner

3/26/2007 11:24:00 PM

0

On Mar 26, 4:53 pm, "Rick DeNatale" <rick.denat...@gmail.com> wrote:
> On 3/25/07, Une Bévue <unbewusst.s...@wortanschahung.com.invalid> wrote:
> > s='toto'
> > s.to_a
> > # => ['toto']
>
> This is because in Ruby a String doesn't act as a collection of
> characters, it acts as a collection of lines.
>
> s = "to\nto"
> s.to_a => ["to\n", "to"]

To be pedantic, it's not really a collection of lines. It's a
collection of stuff-terminated-by-some-arbitrary-character-sequence.
The global variable $/ holds this special string:

>> $/
=> "\n"

>> s = "foo-bar\njim-jam"
=> "foo-bar\njim-jam"

>> s.to_a
=> ["foo-bar\n", "jim-jam"]

>> $/ = '-'
=> "-"
>> s.to_a
=> ["foo-", "bar\njim-", "jam"]

>> $/ = "bar"
=> "bar"
>> s.to_a
=> ["foo-bar", "\njim-jam"]

Rick DeNatale

3/27/2007 4:30:00 PM

0

On 3/26/07, Phrogz <gavin@refinery.com> wrote:
> On Mar 26, 4:53 pm, "Rick DeNatale" <rick.denat...@gmail.com> wrote:
> > On 3/25/07, Une Bévue <unbewusst.s...@wortanschahung.com.invalid> wrote:
> > > s='toto'
> > > s.to_a
> > > # => ['toto']
> >
> > This is because in Ruby a String doesn't act as a collection of
> > characters, it acts as a collection of lines.
> >
> > s = "to\nto"
> > s.to_a => ["to\n", "to"]
>
> To be pedantic, it's not really a collection of lines. It's a
> collection of stuff-terminated-by-some-arbitrary-character-sequence.
> The global variable $/ holds this special string

I think that a better name for
"stuff-terminated-by-some-arbitrary-character-sequence" would be
record. At least the 'english' library seems to think so since it
aliases $RS and $INPUT_RECORD_SEPARATOR to $/

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...