Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6528083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:30:06+00:00 2026-05-25T09:30:06+00:00

I am trying to learn ruby(1.8.7). I have programmed in php for some time

  • 0

I am trying to learn ruby(1.8.7). I have programmed in php for some time and I consider myself proficient in that language. I have a book which I reference, and I have looked at many introductory ruby tutorials, but I cannot figure this out.

myFiles = ['/Users/', '/bin/bash', 'Phantom.file']
myFiles.each do |cFile|
  puts "File Name: #{cFile}"
  if File.exists?(cFile)
    puts "#{cFile} is a file"
    cFileStats = File.stat(cFile)
    puts cFileStats.inspect
    cFileMode = cFileStats.mode.to_s()
    puts "cFileMode Class: " + cFileMode.class.to_s()
    puts "length of string: " + cFileMode.length.to_s()
    printf("Mode: %o\n",  cFileMode)
    puts "User: " + cFileMode[3,1]
    puts "Group: " + cFileMode[4,1]
    puts "World: " + cFileMode[5,1]
  else
    puts "Could not find file: #{cFile}"    
  end
  puts
  puts
end

produces the following output:

File Name: /Users/
/Users/ is a file
#<File::Stat dev=0xe000004, ino=48876, mode=040755, nlink=6, uid=0, gid=80, rdev=0x0, size=204, blksize=4096, blocks=0, atime=Sun Sep 04 12:20:09 -0400 2011, mtime=Thu Sep 01 21:29:08 -0400 2011, ctime=Thu Sep 01 21:29:08 -0400 2011>
cFileMode Class: String
length of string: 5
Mode: 40755
User: 7
Group: 7
World: 


File Name: /bin/bash
/bin/bash is a file
#<File::Stat dev=0xe000004, ino=8672, mode=0100555, nlink=1, uid=0, gid=0, rdev=0x0, size=1371648, blksize=4096, blocks=1272, atime=Sun Sep 04 16:24:09 -0400 2011, mtime=Mon Jul 11 14:05:45 -0400 2011, ctime=Mon Jul 11 14:05:45 -0400 2011>
cFileMode Class: String
length of string: 5
Mode: 100555
User: 3
Group: 3
World: 


File Name: Phantom.file
Could not find file: Phantom.file

Wh is the string length different than expected? (Should be 5 for users, 6 for /bin/bash)? Why are the substrings not puling the correct characters. I understand World not being populated when referencing a 5 character string, but the offsets seem off, and in the case of /bin/bash 3 does not even appear in the string.

Thanks

Scott

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:30:06+00:00Added an answer on May 25, 2026 at 9:30 am

    This is a nice one.

    When a number is preceeded by a 0, it is represented as octal. What you are actually getting for bin/bash:

    0100755 to decimal = 33261
    "33261".length = 5
    

    And for /Users:

    040755 to decimal = 16877
    "16877".length = 5
    

    Add the following line:

    puts cFileMode
    

    And you will see the error.

    to_s takes an argument which is the base. If you call to_s(8) then it should work.

    cFileMode = cFileStats.mode.to_s(8)
    

    EDIT

    files = ['/home/', '/bin/bash', 'filetest.rb']
    files.each do |file|
      puts "File Name: #{file}"
      if File.exists?(file)
        puts "#{file} is a file"
        file_stats = File.stat(file)
        puts file_stats.inspect
        file_mode = file_stats.mode.to_s(8)
        puts "cFileMode Class: #{file_mode.class}"
        p file_mode
        puts "length of string: #{file_mode.length}"
        printf("Mode: #{file_mode}")
        puts "User: #{file_mode[-3,1]}"
        puts "Group: #{file_mode[-2,1]}"
        puts "World: #{file_mode[-1,1]}"
      else
        puts "Could not find file: #{file}"    
      end
      puts
      puts
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying out some sample problems from a learn Ruby book and I'm getting
so, i'm trying to learn ruby by doing some project euler questions, and i've
I am trying to set myself up on a mac to learn Ruby on
Trying to learn about php's arrays today. I have a set of arrays like
I am currently trying to learn Ruby On Rails as I am a long-time
I'm trying to learn some basics of Ruby on Rails and encountered a problem
I am trying to learn Ruby on Rails, I have followed the instructions from
I have recently begun the process of trying to learn Ruby/Rails and have been
Super-beginner easy points ruby question. I'm trying to learn some ruby by programming the
Hi just getting into Ruby, and I am trying to learn some basic file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.