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!
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?!
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)
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)
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!
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.
Open file
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…
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.
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 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:
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?
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?
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…
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/
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