I'm really new to ruby, and I'm trying to do a project that is due soon.
I got the error "undefined method 'new_round' for nil:NilClass" and have no idea about what's going on. Wish someone would help me out. Thanks a lot.
class Blackjack
def initialize()
#deck = Deck.new(gets.to_i())
#dealer = Dealer.new()
#players = []
end
def new_round()
end
end
class Controller
def initializer()
#blackjack = Blackjack.new()
end
def run()
loop do
#blackjack.new_round()
end
end
end
Controller.new().run()
Change initializer() to initialize() :)
You get undefined method 'new_round' for nil:NilClass because #blackjack is nil.
#blackjack is nil because the initializer() method is not being called.
Related
I'm trying to build a tinny DSL using the approach that Russ Olsen exposes in his book, Eloquent Ruby. However it is not working for me. Let's consider the following code:
class SayHello
def initialize
#message = "Hello."
instance_eval(yield) if yield
end
def say_it
puts #message
end
end
SayHello.new { say_it }
The error I get is:
say_hello.rb:12:in `block in <main>': undefined local variable or method `say_it' for main:Object (NameError)
from say_hello.rb:4:in `initialize'
from say_hello.rb:12:in `new'
from say_hello.rb:12:in `<main>'
But... when you use instance_eval method, the value of self shouldn't be assigned to the object that calls the method?
Thanks in advance!
When the block runs, you want self to be equal to your SayHello instance instead of the main object.
I Googled for "ruby change self for a block" and found a good answer which makes me think you should change your code to:
class SayHello
def initialize(&p)
#message = "Hello."
instance_eval(&p) if block_given?
end
def say_it
puts #message
end
end
SayHello.new { say_it }
So, I know there is a simple error, but I just can't seem to spot it. I'm using Modules/Mixins for the first time and any help would be much appreciated. I keep getting this error:
undefined method `this_is' for Value:Module (NoMethodError)
But it looks like the method is there...Here are is my module and classes...
module Value
def this_is
puts "#{self.players_hand} is the players hand"
end
end
require './value.rb'
class Player
include Value
attr_accessor :players_hand
def initialize
#players_hand = 0
end
def value_is
Value.this_is
end
end
require './player.rb'
class Game
def initialize
#player = Player.new
end
def start
puts #player.players_hand
puts #player.value_is
end
end
game = Game.new
game.start
When you include Value inside of the Player class, you are making the Value module's methods a part of the Player class, so the this_is method is not namespaced. Knowing that, we need to change this method:
def value_is
Value.this_is
end
To:
def value_is
this_is
end
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/
I've got some troubles with Ruby about callbacks (and inheritance). Here is my code:
class Lmao
def initialize
#str = "HAHAHAHAHAHHAHAHAH"
#before_laughing = []
end
def self.inherited(base)
base.extend(Callbacks)
end
def laughing
#before_laughing.each {|method| send(method) }
#str
end
end
module Callbacks
def before_laughing(*methods)
#before_laughing = methods
end
end
class Lol < Lmao
before_laughing :downcase_please
def downcase_please
#str.downcase!
end
end
a = Lol.new
a.laughing # => "HAHAHAHAHAHHAHAHAH"
And as you can see, my before laughing callback don't work... because the array #before_laughing is empty. I believe this can be fixed by editing the way I save *methods into an Lol's instance method (from inside Callbacks). But I don't really see how...
If you know the solution, thanks for your light!
Thanks to Mon_Ouie, the solution is:
class Lmao
def initialize
#str = "HAHAHAHAHAHHAHAHAH"
end
def self.inherited(base)
base.extend(Callbacks)
end
def laughing
self.class.callbacks_before_laughing.each {|method| send(method) }
#str
end
end
module Callbacks
def before_laughing(*methods)
#before_laughing = methods
end
def callbacks_before_laughing
#before_laughing
end
end
class Lol < Lmao
before_laughing :downcase_please
def downcase_please
#str.downcase!
end
end
Pretty awesome.
There are two different instance variables called #before_laughing in your code: one is an instance variable of instances of the Lmao class, which gets initialized to [] (i.e. an empty Array) in Lmao's initialize instance methods and gets read in Lmao's laughing instance method. However, since the only place this instance variable gets written to is the initializer, it will always be an empty Array.
The other instance variable is an instance variable of the Lol class object itself, which gets set to the Array [:downcase_please] inside of the before_laughing method. This one, however, never gets read.
I am a bit of a Ruby noob when it comes to the more advanced features. Currently I am experiencing with Proc objects. Can someone tell me what is wrong with this code sample?
class Tester
#printer = Proc.new do |text|
puts text
end
attr_accessor :printer
end
t = Tester.new
t.printer.call("Hello!")
It gives me the following error:
Test.rb:10: undefined method `call' for nil:NilClass (NoMethodError)
I don't immediately see why it shouldn't work. Can someone enlighten me?
You're not setting #printer in the class's initialize method. This'll work:
class Tester
def initialize
#printer = Proc.new { |t| puts t }
end
attr_accessor :printer
end