# Severe newbie question (class, fixnum, array)

**URL:** <https://www.ruby-forum.com/t/severe-newbie-question-class-fixnum-array/106881>\
**Category:** Ruby\
**Created:** [June 14, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/severe-newbie-question-class-fixnum-array/106881 "2007-06-14T23:26:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 14, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/severe-newbie-question-class-fixnum-array/106881/1 "2007-06-14T23:26:16Z")

</div>

I worked a lot in tcl, and this is my first day of trying something in  
Ruby. Second, maybe.

class Thread  
def initialize number  
@number = number ; #  
@replies = 0 ;  
end ; # initialize

def filename  
dirname = “./data/” ; # the directory where the files are kept  
filename = dirname + @number.toS + “-” + @replies.toS + “.html”  
return filename  
end ; # filename

end ; # Thread definition

puts “Finished definitions.”

thread[1727] = Thread.new 1727

puts "The filename would be: "  
puts thread[1727].filename

Of course, that doesn’t work. It is unhappy with the code to create  
the filename, since .toS is not a method for fixnum (which apparently  
is what @number is, but I never TOLD it that was a fixnum).

And I can’t seem to define “thread” to be an array of class Thread  
objects. I want to refer to thread[1702] and thread[291] and hop  
around like that.

(And, perhaps a more advanced question, right now I am making the  
thread number part of the object, ultimately for a gorgeous bit of  
code it seems like the object should just know the index of the array  
instead, so if I ask thread[32] what it’s filename is it would know  
that it is thread #32. Can it do that?)

Thanks. I know it’s a basic question. It’s been a decade since I  
learned a new language. I gotta get out more.

–Colin

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 14, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/severe-newbie-question-class-fixnum-array/106881/2 "2007-06-14T23:35:18Z")

</div>

Perhaps you meant to write the following which would convert the number  
to a  
string?

@replies.to\_s

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 14, 2007, 11:41pm UTC](https://www.ruby-forum.com/t/severe-newbie-question-class-fixnum-array/106881/3 "2007-06-14T23:41:19Z")

</div>

> class Thread

I would say this is a bad idea, since I believe Thread is a core  
class. Call it MyThread, or ThreadDesc or something else.

> def initialize number  
> @number = number ; #  
> @replies = 0 ;  
> end ; # initialize

Get rid of all the semicolons; they are usually not needed.

> def filename  
> dirname = “./data/” ; # the directory where the files are kept  
> filename = dirname + @number.toS + “-” + @replies.toS + “.html”  
> return filename  
> end ; # filename

toS is not an existing method, but to\_s is. Use that, or do this which  
is more compact:

def filename  
“./data/#{@number}-#{@replies}.html”  
end

Couple things here:

1. return is not necessary, as functions return the last evaluation.
2. #{ … } within a string evaluates what is inside the curlies, and  
embeds that in the string.

> thread[1727] = Thread.new 1727

You’ll need to create the array first:  
thread = []  
or  
thread = Array.new

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 14, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/severe-newbie-question-class-fixnum-array/106881/4 "2007-06-14T23:43:20Z")

</div>

Three things:

a) it’s to\_s, as in @number.to\_s  
b) create the array and then insert an element to it:

thread = []  
thread[1] = Thread.new 1

c) to create a file, you could (should?) use file.join, which will  
create the path with the right separators, etc. Look for the rdoc for  
File.join.

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 14, 2007, 11:54pm UTC](https://www.ruby-forum.com/t/severe-newbie-question-class-fixnum-array/106881/5 "2007-06-14T23:54:23Z")

</div>

Hi Colin,

First of all: [http://whytheluckystiff.net/ruby/pickaxe/](http://whytheluckystiff.net/ruby/pickaxe/)

Am 14.06.2007 um 23:25 schrieb Colin S.:

> dirname = “./data/” ; # the directory where the files are kept  
> puts "The filename would be: "  
> puts thread[1727].filename
> 
> Of course, that doesn’t work. It is unhappy with the code to create  
> the filename, since .toS is not a method for fixnum (which apparently  
> is what @number is, but I never TOLD it that was a fixnum).  
> See: [http://whytheluckystiff.net/ruby/pickaxe/html/tut\_stdtypes.html](http://whytheluckystiff.net/ruby/pickaxe/html/tut_stdtypes.html)  
> The method you probably search for is to\_s…

> And I can’t seem to define “thread” to be an array of class Thread  
> objects. I want to refer to thread[1702] and thread[291] and hop  
> around like that.  
> class Thread already exists and is, as its name foreshadows, for  
> [http://whytheluckystiff.net/ruby/pickaxe/html/tut\_threads.html](http://whytheluckystiff.net/ruby/pickaxe/html/tut_threads.html)

> (And, perhaps a more advanced question, right now I am making the  
> thread number part of the object, ultimately for a gorgeous bit of  
> code it seems like the object should just know the index of the array  
> instead, so if I ask thread[32] what it’s filename is it would know  
> that it is thread #32. Can it do that?)  
> class FileThread  
> DIR = ‘data’.freeze # defines class constant  
> @@instances = Array.new # initializes class variable as array

def self.new number # overwrites class method  
@@instances[number] ||= super # set array elem with result from  
parent class if not already set  
end  
def self.[] number # defines new class method  
new number # class class method new  
end

def initialize number  
@number = number  
@replies = 0  
end  
def filename  
# see [http://whytheluckystiff.net/ruby/pickaxe/html/ref\_c\_file.html](http://whytheluckystiff.net/ruby/pickaxe/html/ref_c_file.html)  
@filename ||= File.expand\_path File.join( DIR, “#{ @number }-#  
{ @replies }.html” )  
end  
end

FileThread[1].filename # =\> “#{ Dir.getwd }/data/1-0.html”

Sincerely  
Florian

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 15, 2007, 12:30am UTC](https://www.ruby-forum.com/t/severe-newbie-question-class-fixnum-array/106881/6 "2007-06-15T00:30:30Z")

</div>

Ah, many thanks. Now it works. I should have thought about Thread  
being a predefined class. I thought it highlighted like that because  
it was a constant (what with the capital T I gave it).

Underbars are hard to keep in my head, I think in interCaps.

And the code Florian posted for the object to know it’s index in an  
array… I think that’s probably too difficult for me at this point.  
I’m still bumping around the first project I figured I could use Ruby  
on.

Pickaxe is on my list for today’s trip to the bookstore, and I’ll poke  
around in the first edition a little more. Chris P.'s guide is  
awesome, I just wish he’d write part 2 through 10.

–Colin
