Converting Directory Names to Strings

I’m having a little trouble understanding something. Save the following
script in an empty directory and run it:

Dir.new(".").to_a.each do |x|
puts “x=#{x}”
puts “x.class=#{x.class}”
puts “x[0]=#{x[0]}”
puts
end

You should get output that looks something like this:

x=.
x.class=String
x[0]=46

x=…
x.class=String
x[0]=46

x=listdir.rb
x.class=String
x[0]=108

I was expecting x[0] to be a one-character string.

  1. Why isn’t x[0] a one-character string?
  2. How do I determine what the first character of such a string is? I’m
    guessing this isn’t the proper way, but I’d like to understand why it
    doesn’t work.

Nathan O. wrote:

You should get output that looks something like this:
x.class=String
x[0]=108

I was expecting x[0] to be a one-character string.

  1. Why isn’t x[0] a one-character string?
  2. How do I determine what the first character of such a string is? I’m
    guessing this isn’t the proper way, but I’d like to understand why it
    doesn’t work.

Please look at the String documentation:
http://ruby-doc.org/core/classes/String.html#M001843

If used with a Fixnum, [] returns the ASCII value of the character at
that position.

Try using

x[0,1] to get the first character.

-Justin

I was expecting x[0] to be a one-character string.

Happens a lot

  1. Why isn’t x[0] a one-character string?

Because it returns the character value of the first character.

  1. How do I determine what the first character of such a string is? I’m
    guessing this isn’t the proper way, but I’d like to understand why it
    doesn’t work.

You want to slice the string to return a substring. x[0…0] or
x[0].chr do what you want.

Phillip H. wrote:

  1. Why isn’t x[0] a one-character string?

Because it returns the character value of the first character.

Okay. Is there a good reason for this? If you really want the character
value, why not use an accessor? I’d like to understand the reasoning.

You want to slice the string to return a substring. x[0…0] or
x[0].chr do what you want.

Excellent. Thanks!

On 7/7/06, Nathan O. [email protected] wrote:

Phillip H. wrote:

  1. Why isn’t x[0] a one-character string?

Because it returns the character value of the first character.

Okay. Is there a good reason for this? If you really want the character
value, why not use an accessor? I’d like to understand the reasoning.

Ruby doesn’t provide a character data type, so without this you’d be
unable to find the character value for it.

Phillip H. wrote:

Ruby doesn’t provide a character data type, so without this you’d be
unable to find the character value for it.

What I meant to say is, why not free up x[0] (seems more logical, at
least to me, to use it for tiny strings) and use something like
x[0].ascii or x[0].character_code to return an array of character codes
for all characters in the given string?

It just seems to me that x[0] indicates in no natural, understood way
that it refers to a fixnum, much less one that indicates character
codes.

Nathan O. wrote:

It just seems to me that x[0] indicates in no natural, understood way
that it refers to a fixnum, much less one that indicates character
codes.

You are not alone.
I think it has something to do with something a long time ago? I don’t
know what.

-Justin

Justin C. wrote:

You are not alone.
I think it has something to do with something a long time ago? I don’t
know what.

I don’t mind that it’s quirky, just so long as I’m not the only one who
thinks it’s quirky. Thanks for the assurance that I’m not taking crazy
pills!

My script works now!

On 7/6/06, Nathan O. [email protected] wrote:

Phillip H. wrote:

Ruby doesn’t provide a character data type, so without this you’d be
unable to find the character value for it.
What I meant to say is, why not free up x[0] (seems more logical, at
least to me, to use it for tiny strings) and use something like
x[0].ascii or x[0].character_code to return an array of character codes
for all characters in the given string?

This will be offered in a future Ruby. Look at the long and
often-contentious thread recently about Unicode to see what will be
happening on that end. In Ruby 1.8, “ABC”[0] will return 65; in Ruby
1.9+m17nString (and Ruby 2.0), “ABC”[0] will return “A”.

-austin

On Jul 6, 2006, at 3:54 PM, Nathan O. wrote:

My script works now!


Posted via http://www.ruby-forum.com/.

IIRC this is going to be changed in ruby 1.9 or 2.0 or whatever to
work like you expect.

-Ezra

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs