Forum: Ruby adding anotehr each method to String

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A9e69662b83318b3e00850fc1f910b41?d=identicon&s=25 unknown (Guest)
on 2007-03-25 09:25
(Received via mailing list)
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...
017e05d1a49ffa59ea03e149e7af720b?d=identicon&s=25 Chris Shea (chrisshea)
on 2007-03-25 09:46
(Received via mailing list)
On Mar 25, 2:20 am, unbewusst.s...@wortanschahung.com.invalid (Une
Bévue) wrote:
>
> 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.
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-03-25 10:01
(Received via mailing list)
Might not do what you expect.
  under current conditions you might get:
¨
u

instead of
A9e69662b83318b3e00850fc1f910b41?d=identicon&s=25 unknown (Guest)
on 2007-03-25 10:25
(Received via mailing list)
Chris Shea <cmshea@gmail.com> wrote:

>   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.
A9e69662b83318b3e00850fc1f910b41?d=identicon&s=25 unknown (Guest)
on 2007-03-25 10:27
(Received via mailing list)
John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

> Might not do what you expect.
>   under current conditions you might get:
> ¨
> u
>
> instead of
> ü

then, u mean, it wouldn't work with NON-ASCII chars ?

it doesn't matter in fact because i'm playing with US-ASCII chars only
in that case.

but thanks a lot, better to know !
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-03-25 10:41
(Received via mailing list)
I mean you should check into the way Ruby works with multi-byte
characters!
But this should be watched carefully with every language.
Typing multibyte characters on many windows systems is a pain in the
behind. Luckily it's not so painful on osx, so I can type an umlaut
by option+u then the character I want it over.
Here is what happens...

irb(main):001:0> mystring ="\303\274mlaut"
=> "\303\274mlaut"
irb(main):002:0> puts mystring
ümlaut
=> nil
irb(main):003:0> puts mystring.length
7
=> nil
A9e69662b83318b3e00850fc1f910b41?d=identicon&s=25 unknown (Guest)
on 2007-03-25 11:05
(Received via mailing list)
John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

> I mean you should check into the way Ruby works with multi-byte
> characters!

because i'm a french guy, i knew that )))

but, as writen, for such a tiny script I'll restrict to US-ASCII
converting :

grüß
to

gruess

if y would be a german guy )))

in french it's easier i'll convert all the :

éèêë  to e  und so weiter !

i'll have to check (as for the german ß) for ligatures too :

æ -> ae   again und so weiter !

thanks !
7f891fbe8e3bae7f9fe375407ce90d9d?d=identicon&s=25 Harold Hausman (Guest)
on 2007-03-25 11:29
(Received via mailing list)
On 3/25/07, Une Bévue <unbewusst.sein@wortanschahung.com.invalid> wrote:
>
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
A9e69662b83318b3e00850fc1f910b41?d=identicon&s=25 unknown (Guest)
on 2007-03-25 11:50
(Received via mailing list)
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']
A9e69662b83318b3e00850fc1f910b41?d=identicon&s=25 unknown (Guest)
on 2007-03-25 12:00
(Received via mailing list)
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 )))
> "each_line", "each _with_index", "empty?", "entries", "eql?", "equal?",
> "send", "singleton_method s", "size", "slice", "slice!", "sort",
> Hope that helps,
Yes a lot, thanks again !
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2007-03-25 13:05
(Received via mailing list)
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.
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-03-25 19:34
(Received via mailing list)
Hi,

Am Sonntag, 25. Mär 2007, 16:25:05 +0900 schrieb Une Bévue:
> "zero".each_char{|c| puts c}
>
> would print-out :
> z
> e
> r
> o

The split method was already proposed.

  "another".split //
  "another".split ""
  "another".scan /./
  "änöthér".scan /./u   # will preserve UTF-8 characters

  "another".each_byte { |x| c = x.chr ; ... }
  "another".unpack( "C*")
  "änöthér".unpack( "U*")

Bertram
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-03-26 19:38
(Received via mailing list)
On Mar 25, 2007, at 2:25 AM, 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

#!/usr/bin/env ruby -wKU

require "jcode"

"zero".each_char { |chr| p chr }
# >> "z"
# >> "e"
# >> "r"
# >> "o"

__END__

James Edward Gray II
A9e69662b83318b3e00850fc1f910b41?d=identicon&s=25 unknown (Guest)
on 2007-03-26 20:30
(Received via mailing list)
James Edward Gray II <james@grayproductions.net> wrote:

>
> __END__

thanks to all !
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-27 00:54
(Received via mailing list)
On 3/25/07, Une Bévue <unbewusst.sein@wortanschahung.com.invalid> wrote:
>
> 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.denhaven2.com/
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-03-27 01:26
(Received via mailing list)
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"]
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-27 18:30
(Received via mailing list)
On 3/26/07, Phrogz <gavin@refinery.com> wrote:
> > 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.denhaven2.com/
This topic is locked and can not be replied to.