Realpath() method?

BTW, how is the coordination of the std. lib done?
Is it Matz to harrass with stupid notes like this one?
Or what’s the official way?

Sorry, if I just don’t see the forest for the tree, but do
we have some “normalize path” method somewhere
in the std. stuff (like realpath() of PHP)?

Thanks!
Sab

How to elegantly get the last char of a string as a string?
Somehow I fear I won’t be cacthing girls’ attention with this:

if ('' << str[str.length-1]) = 'x'
    str.chop
    do_things_with(str)
    ...
end

Thanks,
Sab

Sorry, if I just don’t see the forest for the tree, but do
we have some “normalize path” method somewhere
in the std. stuff (like realpath() of PHP)?

For the archive: File.expand_path

Sab

How to elegantly get the last char of a string as a string?
Somehow I fear I won’t be cacthing girls’ attention with this:

if ('' << str[str.length-1]) = 'x'
    str.chop
    do_things_with(str)
    ...
end

ibz@ignoramus:$ irb
Reading config file… done.
ruby-1.7.3 on i686-pc-linux with irb 0.9(02/07/03)
Type ‘help’ for more information.

str = “This is a string”
“This is a string”
str[-1].chr
“g”

Is that what you are looking for?

		--ibz.
···

Ahhh, yes! Thanks!

I was reading the Pragmatic… book and the String class
doesn’t have that ‘chr’ method there. (And a special thanks
for the negative indexes, which I missed completely…)

Thanks again,
Sab

···

----- Original Message -----
From: “Ibraheem Ibz Umaru-Mohammed” iumarumo@btinternet.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, October 19, 2002 9:48 PM
Subject: Re: string[lastchar] → string?

How to elegantly get the last char of a string as a string?
Somehow I fear I won’t be cacthing girls’ attention with this:

if ('' << str[str.length-1]) = 'x'
    str.chop
    do_things_with(str)
    ...
end

ibz@ignoramus:$ irb
Reading config file… done.
ruby-1.7.3 on i686-pc-linux with irb 0.9(02/07/03)
Type ‘help’ for more information.

str = “This is a string”
“This is a string”
str[-1].chr
“g”

Is that what you are looking for?

–ibz.

(OK, I’m doing great today… Of course the String
class doesn’t have str() method… Er…, I was just asking
it for a friend of mine… :wink: )

Sab

···

----- Original Message -----
From: “Szabolcs Szasz” sz@szasz.hu
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, October 19, 2002 9:55 PM
Subject: Re: string[lastchar] → string?

Ahhh, yes! Thanks!

I was reading the Pragmatic… book and the String class
doesn’t have that ‘chr’ method there. (And a special thanks
for the negative indexes, which I missed completely…)

Thanks again,
Sab

----- Original Message -----
From: “Ibraheem Ibz Umaru-Mohammed” iumarumo@btinternet.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, October 19, 2002 9:48 PM
Subject: Re: string[lastchar] → string?

How to elegantly get the last char of a string as a string?
Somehow I fear I won’t be cacthing girls’ attention with this:

if ('' << str[str.length-1]) = 'x'
    str.chop
    do_things_with(str)
    ...
end

ibz@ignoramus:$ irb
Reading config file… done.
ruby-1.7.3 on i686-pc-linux with irb 0.9(02/07/03)
Type ‘help’ for more information.

str = “This is a string”
“This is a string”
str[-1].chr
“g”

Is that what you are looking for?

–ibz.

Ahhh, yes! Thanks!

I was reading the Pragmatic… book and the String class
doesn’t have that ‘chr’ method there. (And a special thanks
for the negative indexes, which I missed completely…)

I usually just fire up the interpreter, and do something like:

ibz@ignoramus:$ irb
Reading config file… done.
ruby-1.7.3 on i686-pc-linux with irb 0.9(02/07/03)
Type ‘help’ for more information.

String.methods.sort
[“<”, “<=”, “<=>”, “==”, “===”, “=~”, “>”, “>=”, “id”, “send”, “allocate”, “ancestors”, “become”, “class”, “class_eval”, “class_variables”, “clone”, “const_defined?”, “const_get”, “const_set”, “constants”, “display”, “dup”, “eql?”, “equal?”, “extend”, “freeze”, “frozen?”, “hash”, “id”, “include?”, “included_modules”, “inspect”, “instance_eval”, “instance_method”, “instance_methods”, “instance_of?”, “instance_variables”, “is_a?”, “kind_of?”, “method”, “method_defined?”, “methods”, “module_eval”, “name”, “new”, “nil?”, “private_class_method”, “private_instance_methods”, “private_methods”, “protected_instance_methods”, “protected_methods”, “public_class_method”, “public_instance_methods”, “public_methods”, “respond_to?”, “send”, “singleton_methods”, “superclass”, “taint”, “tainted?”, “to_a”, “to_s”, “type”, “untaint”]

Which helps a little when The Book isn’t nearby…

How to elegantly get the last char of a string as a string?
Somehow I fear I won’t be cacthing girls’ attention with this:

if ('' << str[str.length-1]) = 'x'
    str.chop
    do_things_with(str)
    ...
end

[— snip —]

Also, although you won’t be using the code above, you’ll find you’ve
forgotten a “=” and a “!” somewhere in there :wink:

		--ibz.
···

Well, you don’t really need the ‘chr’ method, which is a method of class
Integer. As long as you know about the negative indices, you can also
write

str[-1, 1]

This is related to item 5 in the newcomers’ list
(http://www.glue.umd.edu/~billtj/ruby.html#Getting%20characters%20from%20a%20String)

Regards,

Bill

···

===========================================================================
Szabolcs Szasz sz@szasz.hu wrote:

Ahhh, yes! Thanks!

I was reading the Pragmatic… book and the String class
doesn’t have that ‘chr’ method there. (And a special thanks
for the negative indexes, which I missed completely…)

I was reading the Pragmatic… book and the String class
doesn’t have that ‘chr’ method there.

That’s because it’s in the Integer class because characters are just
integer (codes).

str = “This is a string.”
“This is a string.”
str[-1]
46
str[-1].chr
“.”

···


Greg McIntyre
greg@puyo.cjb.net
http://puyo.cjb.net

There’s also ri

$ ri String

···

-----Original Message-----
From: ibraheem@ignoramus [mailto:ibraheem@ignoramus]On Behalf Of
Ibraheem Ibz Umaru-Mohammed
Sent: Saturday, October 19, 2002 1:03 PM
To: ruby-talk ML
Subject: Re: string[lastchar] → string?

Ahhh, yes! Thanks!

I was reading the Pragmatic… book and the String class
doesn’t have that ‘chr’ method there. (And a special thanks
for the negative indexes, which I missed completely…)

I usually just fire up the interpreter, and do something like:


 class: String

 A String object holds and manipulates an arbitrary sequence of
 bytes, typically representing characters. String objects may be
 created using String::new or as literals (see page 204).
 Because of aliasing issues, users of strings should be aware of the
 methods that modify the contents of a String object. Typically,
 methods with names ending in ``!'' modify their receiver, while
 those without a ``!'' return a new String. However, there are
 exceptions, such as String#[]=.

 %, *, +, <<, <=>, ==, ===, =~, [], []=, capitalize, capitalize!,
 center, chomp, chomp!, chop, chop!, concat, count, crypt, delete,
 delete!, downcase, downcase!, dump, each, each_byte, each_line,
 empty?, gsub, gsub!, hash, hex, include?, index, intern, length,
 ljust, new, next, next!, oct, replace, reverse, reverse!, rindex,
 rjust, scan, size, slice, slice!, split, squeeze, squeeze!, strip,
 strip!, sub, sub!, succ, succ!, sum, swapcase, swapcase!, to_f,
 to_i, to_s, to_str, tr, tr!, tr_s, tr_s!, unpack, upcase, upcase!,
 upto, ~

BTW, chr is not part of String, but belongs to Integers

$ ri chr
------------------------------------------------------------ Integer#chr
int.chr → aString

 Returns a string containing the ASCII character represented by the
 receiver's value.
    65.chr    #=> "A"
    ?a.chr    #=> "a"
    230.chr   #=> "\346"

James

“Ibraheem Ibz Umaru-Mohammed” iumarumo@btinternet.com wrote in message
news:20021019200233.GB5546@micromuse.com

I usually just fire up the interpreter, and do something like:

ibz@ignoramus:$ irb
Reading config file… done.
ruby-1.7.3 on i686-pc-linux with irb 0.9(02/07/03)
Type ‘help’ for more information.

String.methods.sort

try

ri String

from the shell, only in 1.7

Mikkel