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
Related
I am ruby beginner. I am use proc class but I am getting error.
class Timeline
attr_accessor :tweets
def each(&block) # Block into the proc
tweets.each(&block) # proc back into the block
end
end
timeline = Timeline.new(tweets)
timeline.each do |tweet|
puts tweet
end
Getting error :-
`each': undefined method `each' for nil:NilClass (NoMethodError)
How to solve this error? Please tell us!
When you define attr_accessor :tweets, you just define 2 instance methods:
def tweets
#tweets
end
def tweets=(tweets)
#tweets = tweets
end
When you call tweets inside the each method, you just call method with this name, not a local variable, so you should set #tweets in the initialize method because right now your #tweets variable is not set:
class Timeline
attr_accessor :tweets # this is just a nice syntax for instance variable setter
# and getter
def initialize(tweets)
#tweets = tweets
end
def each(&block) # Block into the proc
tweets.each(&block) # proc back into the block
end
end
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 }
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.
I found out this morning that proc.new works in a class initialize method, but not lambda. Concretely, I mean:
class TestClass
attr_reader :proc, :lambda
def initialize
#proc = Proc.new {puts "Hello from Proc"}
#lambda = lambda {puts "Hello from lambda"}
end
end
c = TestClass.new
c.proc.call
c.lambda.call
In the above case, the result will be:
Hello from Proc
test.rb:14:in `<main>': undefined method `call' for nil:NilClass (NoMethodError)
Why is that?
Thanks!
The fact that you have defined an attr_accessor called lambda is hiding the original lambda method that creates a block (so your code is effectively hiding Ruby's lambda). You need to name the attribute something else for it to work:
class TestClass
attr_reader :proc, :_lambda
def initialize
#proc = Proc.new {puts "Hello from Proc"}
#_lambda = lambda {puts "Hello from lambda"}
end
end
c = TestClass.new
c.proc.call
c._lambda.call
class Card
attr_accessor :number, :suit
def initialize(number, suit)
#number = number
#suit = suit
end
def to_s
"#{#number} of #{#suit}"
end
end
I'm assuming this creates a new array correct?
But why the use of the AT symbol? When should I use it and not use it?
#stack_of_cards = []
#stack << Card.new("A", "Spades")
puts #stack
# => BlackjackGame.rb:17: undefined method `<<' for nil:NilClass (NoMethodError)
Any ideas why this error is firing?
Exactly as it says in error: variable #stack is not defined (or nil).
Did you mean #stack_of_cards << .. instead?
If you had warnings on (ruby -W2 script_name.rb), you would have got a warning that #stack is not merely nil, but undefined. See How do I debug Ruby scripts? for more hints on how to debug.