Topic: needing help

kinda difficult to explain in the title, but...
i am needing help getting my short script to display one of five options rather than just the last option, every time its run.
i even switched it to directly display an alternate choice and it still only shows the last. to save space the idea of the code is...

class Cls1
   end
   @itema = txt
class Cls2
   end
   @itema = txt1
class Cla1 < Cls1
   end
   @itemb = dot
class Cla2 < Cls2
   end
   @itemb = dot1

X = 1 + rand(2)

case X
   when 1 then Cla1.new
   when 2 then Cla2.new
end

puts @itema
puts @itemb

::::::::
roughly the example of the code only with more options, however it only displays the last possible choice even when i manually set X to another value...so yea thats what i am needing help with. I have tried the If else, elsif statements with same results.

Thanks in advance.

Last edited by wolfblod (2011-11-05 06:46:43)

Re: needing help

Take a look at what you have written.  All of your assignments are outside of the class definitions.  Is that what you want?  If it is then what service are all of those empty classes providing?
The way it is written it should always print txt1 and dot1.  The classes do not do anything and have nothing to do with the variables.

Re: needing help

Well that does explain why its returning the same thing regardless, how ever... after moving the ends so that the information is included into the classes, its not displaying the information in the strings.
The classes are there to provide extra base line information, so instead of having to fill out all the information per object it can pick up the details along the way and reduce the amount of coding, and writing, needed in the program.

As for the puts, they were there because it should have been, in the example, puts( " txt is #{@itema}" )
to put it into context, Cls is the band and Cla is the album. further down the ladder would be the song name. So when the program is run it would put,
Band @itema
Album @itemb
Song @itemc

Also i used puts( "txt is #{@itema}" ) taken from an example program from the little book of ruby.

Last edited by wolfblod (2011-11-07 18:51:56)

Re: needing help

@itema is not the same as Cla1.new.itema

In your code you have two @itema objects.  One is an attribute of an object of class Cls1 and another is an attribute of an object of class Cls2.  In your code you created unnamed objects of two classes.  The creation could have some side effects (bad idea) but in the case you defined it just created some objects which have no names so you cannot do anything with them.  You need to study up on how objects and classes really work.

Assuming that txt and dot are in scope when the classes are defined...  You first need to create objects which are members of the class you want 

xx=Cla1.new

and then you can use the attributes of that class

puts "txt is #{xx.itema}"

Re: needing help

I guess i will have to do a lot more reading since i read this was an easy language to use, however Game Makers language and the Texas Instruments Basic language are so much simpler to use. I thought the classes were for declaring values for variables and not objects. I could avoid this trouble if i could actually find the scripts used in the examples in the little book of ruby but i cant seem to find them separate of downloading another copy of ruby. any help there would be appreciated. This script is supposed to be a simple one pulling the general information from a tree so each object doesn't have to have all the information in it.

Re: needing help

Ruby is a powerful and expressive language but I would not say it is easy unless you have a background in things like perl and c++ or some other object oriented language.  I have been a professional programmer for over 30 years and I found Ruby to be  a fun language to explore but not easy.  Every thing in Ruby is  an object even a string.  A piece of code like

"abc".reverse

demonstrates that "abc" is an object and it has a set of methods (all of the methods associated with string and basic object).  This makes Ruby very powerful and useful but not necessarily easy.  I am sure Game Makers Language and some version of Basic are easy but they are domain specific languages while Ruby is a general purpose scripting language.

C is probably the easiest language around because it is simple and consistent but that doesn't mean it is necessarily the easiest to do complex things in.

You need to evaluate a language by how it answers your needs.  If all you ever want to do is to program some set of games or applications that the languages you cited can support, by all means use them and be happy with them.  If you want to learn and expand your capabilities so you can do other things Ruby is a good language to learn as are Java and C++ and other similar languages.  If you really want to learn about Ruby I would suggest the Pickaxe book "Programming Ruby 1.9" by Dave Thomas et al.  It will take you a while to get through most of it but it will be rewarding.

Good Luck
Norm

Re: needing help

I would like to learn more languages however i have found that i learn better with trial and error, i dont learn much from reading, because in the reference material i have, it gives examples as i have shown and they work. example is the Dogs and Cats script, however i will look into the book you suggested, as i only have a few years experience and only with the 2 languages i listed.

Re: needing help

The ONLY thing that would make Ruby and Rails easy to learn is if you had a background in SmallTalk-80!  In 1987 I was fortunate enough to have a gig where I evaluated Smalltalk-80 on a Sun workstation,  which meant for 5 months I played with it.  Parc Place Systems didn't even have a tech support program set up,  I called to inquire about purchasing a tech support 'license' and they were like, "err..  Uh..,  we havn't done that yet,  we'll have  get back to you",  I finally got them to take the companies 5 grand,  and when I did call for support,  they had to run around and find an engineer who would take the call!!

Smalltalk-80 is like Ruby with the MVC paradigm built into the language,  the block syntax of Ruby is almost identical to Smalltalk-80's.  Ruby has a much richer set of functions,  you'd of had to spent years programming in SMalltalk-80 to give it the rich API  Ruby has out of the box.  Otherwise,  a very similar experience, minus the HTML/CSS and JavaScript of course.

SMalltalk-80 was SO ahead of it's time!

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: needing help

I guess I missed out but we didn't do any smalltalk.  Sounds fun as long as you didn't need support.

I have put together a set of classes which appear to be what wolfblod was trying to do.  The class initialization is a bit simpler than c++ but I am sure complex along side of Basic (which I haven't used since the '70s).  In Ruby on Rails ActiveRecord classes take care of a lot of the details for you and automagicly give you methods to access and set each column in the database records.

Here is how I would set it up....

class Cls1
  def initialize # initialize is where you initialize things.  Called by new
    @itema = "txt"
  end
  def itema # you need to define a method to return a value from a class object
    @itema
  end
end

class Cls2
  attr_reader :itema # simpler way of defining a method to return a value from a class object
  def initialize # every class that wants to have data initialized in it needs initialize
    @itema = "txt1"
  end
end

class Cla1 < Cls1 # Cla1 inherits from Cls1
  attr_reader :itemb
  def initialize
    @itemb = "dot"
    super # needed if you want to call the parents initialize method
  end
end

class Cla2 < Cls2
  attr_reader :itemb
  def initialize
    @itemb = "dot1"
    super
  end
end