I have created an item class and am trying to rake test it. When running the code outside the test no errors are thrown. Because of this i assume i am testing it wrong.
class Item
attr_accessor :name, :description, :item
def initialize (item, description, name)
#name = item[:name]
#description = item[description]
end
end
And the code i am using to test it is
require "Asheron's_call/Item.rb"
require "Test/Unit"
class TestGame < Test::Unit::TestCase
def test_item
one = Item.new ("Potion","Red")
assert_equal("Potion", one.name)
end
end
=>93: one = Item.new ("Potion","Red")
94: assert_equal("Potion", one.name
when running the test with that i am getting a new error which is a syntax error. It is expecting a ')' after potion. When i changed that to see what would happen and then came back saying it expected me to place 'end' which to me feels wrong.
The test is fine. The Item’s constructor is not:
class Item
attr_accessor :name, :description
def initialize (name, description)
#name = name
#description = description
end
end
There is no item there. Also, in the test one should assert the instance, not the class:
# wrong: assert_equal("Potion", Item.name)
assert_equal("Potion", one.name)
Related
I am learning Ruby but I am currently stuck here when trying to create a new object inside a method from another class. I have this main 'command.rb' class where I just initialize its arguments:
class Command
attr_accessor :key, :flag, :time, :bytes, :value,
def initialize(key, flag, expTime, bytes,value)
#key = key
#flag = flag
#expTime = expTime
#bytes = bytes
#value = value
end
end
Then I try to create a Command object inside this other class:
require_relative 'command'
class CommandDAO
def initialize
#data_hash = Hash.new
end
def set(arrayInfo, value)
full_key = Command.new(arrayInfo[1],arrayInfo[2],arrayInfo[3],arrayInfo[4],value)
data_hash.store(key,full_key)
return "STORED\r\n"
end
end
The error I am getting at the moment is that the expect number of values is 0. Why is this?
Thanks a lot for your help!!
Remove the comma after value
attr_accessor :key, :flag, :time, :bytes, :value,
It causes your initializer body to be regenerated/overwritten by attr_accessor
So I have the following setup
class foo
:attr_accessor :textBoxInFoo
#textBoxInFoo
def appendText
//appends text to textBoxInFoo
end
def getLastPartOfText
//gets the last line of text in textBoxInFoo
end
end
class bar
def UseFoo
//Declares instance of textBoxInFoo
end
end
class snafu
def runInBackground
//Needs to read and write from instance of textBoxInFoo in bar class
end
end
What I'm failing to grasp is how to do what I need to do in runInBackground to get it to read and write to textBoxInFoo, I've been reading through various explanations of instance and class methods, and none of them have really clicked with me yet, so I'm wondering if anyone knows what I'm messing up.
This is a small example of how you can create a user object and send it to student object as an argument. run() method calls user's run method.
class User
attr_accessor :name
def initialize(name) # it is similar to constructor
#name = name
end
#run method
def run
puts "I am running"
end
#getter for name
def get_name
#name
end
#setter for name
def set_name=(name)
#name = name
end
end
class Student
attr_accessor :obj
def initialize(obj)
#obj = obj
end
def run
obj.run
puts "inside of Student"
end
end
user = User.new("John")
stud = Student.new(user)
stud.run # shows I am running
# inside of student
class Books
attr_accessor :name, :book_id
def initialize(name, book_id)
#name = name,
#book_id = book_id
end
end
class BookCollection
def intialize
#book_names = []
end
def add_to_books(book_name)
book_name.push(book_names)
end
end
book1 = Books.new("catch22", "12345")
book_collection1 = BookCollection.new
book_collection1.add_to_books(book1.name)
puts book_collection1
end
That is my code and the error I'm getting is "undefined local variable or method `book_names'". I tried adding " attr_accessor :book_names" and when I do that the printed output doesn't make sense.
There are a few mistakes in your code:
line 4 should not end with a comma.
initialize in class BookCollection is misspelled, resulting in #book_names not being initialized. #book_names therefore equals nil when you attempt to add an element to it with push. nil does not have a method push; hence the exception, and the message printed with the exception.
book_name.push(book_names) should be #book_name.push(book_name). (#book_name must be an instance_variable, as opposed to a local variable, to be visible outside a method, within the class definition.
puts book_collection1 prints the class instance; you want to print #book_names.
Here I've fixed your code. I've used << instead of push. Either is OK, but the former seems to be favored my most.
class Books
attr_accessor :name, :book_id
def initialize(name, book_id)
puts "name = #{name}, book_id = #{book_id}"
#name = name
#book_id = book_id
end
end
class BookCollection
attr :book_names
def initialize
#book_names = []
end
def add_to_books(book_name)
#book_names << book_name
end
end
book_collection1 = BookCollection.new
book1 = Books.new("Catch22", "12345")
book2 = Books.new("Hawaii", "67890")
book_collection1.add_to_books(book1.name)
book_collection1.add_to_books(book2.name)
book_collection1.book_names # => ["Catch22", "Hawaii"]
Probably just a typo at
book_name.push(book_names)
Should have been
book_names.push(book_name)
With attr_accessor :book_names
I'm trying to follow a lesson with the ruby class below, I just don't understand how the results of the output statement is "John Smith" from calling the player function followed by new variable?
Is there any simpler way for this? the coder did it in a way that i got confused
Lastly, Can you tell me how to debug Ruby Class or any ruby code on TextMate? I mean debug just like debugging in Visual C++ shows me what first line gets called and excauted and then jumps to the next etc... to see how it works?
class Dungun
attr_accessor :player
def initialize(player_name)
#player = Player.new(player_name)
#rooms = []
end
class Player
attr_accessor :name, :location
def initialize(player_name)
#name = player_name
end
end
class Room
attr_accessor :reference, :name, :description, :connection
def initialize(reference,name,description,connection)
#reference = reference
#name = name
#description = description
#connection = connection
end
end
end
my_dungun = Dungun.new("John Smith")
puts my_dungun.player.name
Execution order
# 1. Called from my_dungun = Dungun.new("John Smith")
Dungun.new("John Smith")
# 2. Inside Dungun it will call the initialize from Dungun class
initialize("John Smith")
# 3. The initialize method, from Dungun class, will have this statement saying
# that instance variable #player will receive the
# result of Player.new("John Smith")
#player = Player.new("John Smith")
# 4. The Player's 'new' method will call the
# inner class Player's initialize method
initialize("John Smith")
# 5. The Player's initialize should assign "Jonh Smith" to #player's name
#name = "John Smith"
# 6. Then head back to where we stopped, and continue to the other
# statement at second line inside Dungun's 'new' method
#rooms = []
And read Mastering the Ruby Debugger for a ruby debug gem and some lessons!
I'm new to Ruby and trying to determine how I can call a class from a child object. Something like the below; however when I try it, I get an error saying "undefined local variable or method `me'"
class my_object < Object
attr_accessor :me
def initialize(attributes ={})
end
def setvalue(passed_value)
#passed_value = passed_value.to_s
end
def search(passed_value)
#passed_value.include?(passed_value)
end
end
def getMe
me_too = my_object.new
me_too.me = "test"
me_too.me.search("test")
end
end
instance.class
will give you a reference to the class
This works:
But your code had multiple errors.
class MY
attr_accessor :me
def initialize(attributes ={})
end
def setvalue(passed_value)
passed_value = passed_value.to_s
end
def search(passed_value)
passed_value.include?(passed_value)
end
def getMe
me_too = MY.new
me_too.me = "test"
me_too.search("test")
end
end
my = MY.new
my.getMe
You don't need to explicity extend Object, everything extends Object in ruby.
Your class name needs to start with a capital letter.
class MyObject
attr_accessor :me
end
me_too = MyObject.new
me_too.me = "test"
in console
me_too => #<MyObject:0x106b2e420 #me="test">
Check out some introductory ruby tutorials maybe http://ruby.learncodethehardway.org/