# Ruby in the sky with diamonds

**URL:** <https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752>\
**Category:** Ruby\
**Created:** [June 18, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752 "2006-06-18T20:18:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![banach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@banach](https://www.ruby-forum.com/u/banach)\
**Post date:** [June 18, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752/1 "2006-06-18T20:18:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![banach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@banach](https://www.ruby-forum.com/u/banach)\
**Post date:** [June 18, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752/2 "2006-06-18T20:34:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![banach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@banach](https://www.ruby-forum.com/u/banach)\
**Post date:** [June 18, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752/3 "2006-06-18T20:59:15Z")

</div>

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/)  
[http://www.whytheluckystiff.net/ruby/pickaxe/html/tut\_io.html](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)  
[http://ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html)

matthew smillie.

---

<div class="post-metadata">

**Author:** ![banach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@banach](https://www.ruby-forum.com/u/banach)\
**Post date:** [June 18, 2006, 10:04pm UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752/4 "2006-06-18T22:04:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![banach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@banach](https://www.ruby-forum.com/u/banach)\
**Post date:** [June 18, 2006, 10:10pm UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752/5 "2006-06-18T22:10:36Z")

</div>

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](http://ruby-doc.org/core/classes/YAML.html)

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![banach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@banach](https://www.ruby-forum.com/u/banach)\
**Post date:** [June 18, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752/6 "2006-06-18T22:15:34Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![banach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@banach](https://www.ruby-forum.com/u/banach)\
**Post date:** [June 18, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752/7 "2006-06-18T22:22:54Z")

</div>

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/](http://www.ruby-doc.org/)

---

<div class="post-metadata">

**Author:** ![banach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@banach](https://www.ruby-forum.com/u/banach)\
**Post date:** [June 18, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752/8 "2006-06-18T23:12:28Z")

</div>

On 18/06/06, Timothy H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![banach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@banach](https://www.ruby-forum.com/u/banach)\
**Post date:** [June 19, 2006, 12:49am UTC](https://www.ruby-forum.com/t/ruby-in-the-sky-with-diamonds/63752/9 "2006-06-19T00:49:46Z")

</div>

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
