Ruby in the sky with diamonds

Hey there, sorry I’m a little new to ruby and need a tiny bit of help if
anyone can see to help thatd be great:

I better warn you now that this probably isnt going to be a short post!

  1. Say I have 5 files which just contains arrays, hashes or a mixture
    of both and I want to let somebody choose one of them: for example

Code:
puts “Please make a choice from the 5 files”
choice = gets.chomp

then I want ruby/irb to output the hashes/arrays contained in the
files, like the cat or less commands with bash, how do I go about this?!

  1. I have created a qtruby/kde ruby (I use kdevelop so chose a kde
    application from the project wizard) project and created the main window
    and given it a title/buttons etc but now I want to partition the main
    window into 4 or 5 smaller boxes which will display information (as
    opposed to taking data) upon clicking (I know how to caputre the mouse
    so thats not a problem) so how would I go about partitioning the window
    into these boxes? (the size and position commands I’m safe with I just
    need to know how to create the subwindows)

  2. An easier one methinks, how do I get irb to put what the contents of
    a folder are and whether they are folders or files? (again kind of like
    ls in bash)

Cheers guys and gals

On Mon, Jun 19, 2006 at 03:22:15AM +0900, Gareth Clews wrote:

Hey there, sorry I’m a little new to ruby and need a tiny bit of help if
anyone can see to help thatd be great:

First of off there’s ignorance all throughout this post.

I better warn you now that this probably isnt going to be a short post!

  1. Say I have 5 files which just contains arrays, hashes or a mixture
    of both and I want to let somebody choose one of them: for example

Um, files don’t contain a data structure. You read data in from files
into a
data structure (array, hash, etc…).

Code:
puts “Please make a choice from the 5 files”
choice = gets.chomp

Um, this is same concept as any language.

  1. Open file
  2. Read into data structure

then I want ruby/irb to output the hashes/arrays contained in the
files, like the cat or less commands with bash, how do I go about this?!

Iterate over the data structure and use one of the IO methods:
puts, print, printf, etc…

  1. I have created a qtruby/kde ruby (I use kdevelop so chose a kde
    application from the project wizard) project and created the main window
    and given it a title/buttons etc but now I want to partition the main
    window into 4 or 5 smaller boxes which will display information (as
    opposed to taking data) upon clicking (I know how to caputre the mouse
    so thats not a problem) so how would I go about partitioning the window
    into these boxes? (the size and position commands I’m safe with I just
    need to know how to create the subwindows)

Why post this to a Ruby list? This is a kdevelop/qt item and needs to
be
post to an appropriate list.

  1. An easier one methinks, how do I get irb to put what the contents of
    a folder are and whether they are folders or files? (again kind of like
    ls in bash)

Need to gain more about basic principals. If you don’t understand
reading of
files and such same goes for getting the directories contents.

Please see the programming ruby to clear up many of the falsities in
this post
before asking for help.

On Jun 18, 2006, at 19:33, Cliff C. wrote:

On Mon, Jun 19, 2006 at 03:22:15AM +0900, Gareth Clews wrote:

Hey there, sorry I’m a little new to ruby and need a tiny bit of
help if
anyone can see to help thatd be great:

First of off there’s ignorance all throughout this post.

And little useful information in the reply - it’s all well and good
saying “look things up before you post”, but it’s usually common
courtesy to at least show people where to look.

Basically, irb isn’t a shell - on its own, it doesn’t list files or
directories, it just executes ruby statements. Ruby can do these
things, though, and here’s how to find out:

For I/O in general, useful things to read are the relevant chapter of
Programming Ruby (“Basic Input and Output”), available online here:
http://www.whytheluckystiff.net/ruby/pickaxe/
http://www.whytheluckystiff.net/ruby/pickaxe/html/tut_io.html

The Pathname library is my favourite for this sort of thing, docs here:
http://ruby-doc.org/stdlib
http://ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html

matthew smillie.

Cliff C. wrote:

On Mon, Jun 19, 2006 at 03:22:15AM +0900, Gareth Clews wrote:

Hey there, sorry I’m a little new to ruby and need a tiny bit of help if
anyone can see to help thatd be great:

First of off there’s ignorance all throughout this post.

Perhaps be a little kinder? He said himself he was new to Ruby.

Gareth Clews wrote:

  1. Say I have 5 files which just contains arrays, hashes or a mixture
    of both and I want to let somebody choose one of them […]
    then I want ruby/irb to output the hashes/arrays contained in the
    files, like the cat or less commands with bash, how do I go about this?!

Do you mean you’re storing data in ruby arrays and hashes? You might
want to take a look at YAML[1]. Are the file names static, and is there
a fixed amount of files?

  1. An easier one methinks, how do I get irb to put what the contents of
    a folder are and whether they are folders or files? (again kind of like
    ls in bash)

Well, I’m sure it’s not too hard, but I don’t understand why you want
to. Are you using that information for something, or do you just want to
browse the directories?

Remember, IRB isn’t a terminal.

[1] http://ruby-doc.org/core/classes/YAML.html

Cheers,
Daniel

Daniel S. wrote:

Gareth Clews wrote:

  1. Say I have 5 files which just contains arrays, hashes or a mixture
    of both and I want to let somebody choose one of them […]
    then I want ruby/irb to output the hashes/arrays contained in the
    files, like the cat or less commands with bash, how do I go about this?!

Do you mean you’re storing data in ruby arrays and hashes? You might
want to take a look at YAML[1]. Are the file names static, and is there
a fixed amount of files?

Yeah, there are a fixed number of files with static file names and
essentially each file is just a text file with the text being arrays and
hashes.

Cheers for the help guys (especially YAML and where to look for the
help)
I nearly got put off by the guy who didn’t leave his name…

Gareth Clews wrote:

  1. An easier one methinks, how do I get irb to put what the contents of
    a folder are and whether they are folders or files? (again kind of like
    ls in bash)

Easy way: system(“ls”).

Harder way, use the Dir class or the Find module. Dir is one of the core
classes. Find isn’t, but it is part of the standard library so all you
have to do to use it is execute

require ‘find’

in irb before using any of the Find methods. Both Dir and Find are
documented here: http://www.ruby-doc.org/

On 18/06/06, Timothy H. [email protected] wrote:

Gareth Clews wrote:

  1. An easier one methinks, how do I get irb to put what the contents of
    a folder are and whether they are folders or files? (again kind of like
    ls in bash)

Easy way: system(“ls”).

Harder way, use the Dir class or the Find module. Dir is one of the core
classes. Find isn’t, but it is part of the standard library so all you
have to do to use it is execute

Here’s a fairly easy way that doesn’t need anything from the standard
library:

Dir[‘*’].each do |entry|
puts “#{entry}#{File.directory?(entry) ? ‘/’ : ‘’}”
end

Paul.

Cliff C. wrote:

On Mon, Jun 19, 2006 at 03:22:15AM +0900, Gareth Clews wrote:

Hey there, sorry I’m a little new to ruby and need a tiny bit of help if
anyone can see to help thatd be great:

First of off there’s ignorance all throughout this post.

Well, that depends on what “this post” is referencing.


Need to gain more about basic principals. If you don’t understand reading of
files and such same goes for getting the directories contents.

Please see the programming ruby to clear up many of the falsities in this post
before asking for help.

Gareth, if you have questions, please ask. Ask me directly (james dot
britt at gmail dot com) if you like.

Please don’t let this aberrant “welcome” give you the wrong idea about
the Ruby community.


James B.

“You harmonize; then you customize.”

  • Wilson Pickett