newbie ..need help with metaprogramming..

D

Dhaval

hi, i wrote a small code to check metaprogramming in ruby..what am i
doing wrong..can you pl help?

class Testvar
attr_accessor :a1
def initialize
@a1='init'
end
end

var =":a1"

t = Testvar.new
if t.respond_to?("#{var}")
p 'test 1- pass'
end
if t.instance_variable_set var , 'abc'
p 'test 2-pass'
end
if t.send var , 'def'
p 'test 3-pass'
end
 
C

Caleb Clausen

hi, i wrote a small code to check metaprogramming in ruby..what am i
doing wrong..can you pl help?

class Testvar
attr_accessor :a1
def initialize
@a1='init'
end
end

var =":a1"

t = Testvar.new
if t.respond_to?("#{var}")
p 'test 1- pass'
end
if t.instance_variable_set var , 'abc'
p 'test 2-pass'
end
if t.send var , 'def'
p 'test 3-pass'
end

What did you expect it to do, and what did it actually do?
I'm guessing that it blows up at instance_variable_set, because a1 is
not a valid ivar name. Try with an @ instead:

t.instance_variable_set("@#{var}", 'abc')
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

hi, i wrote a small code to check metaprogramming in ruby..what am i
doing wrong..can you pl help?

class Testvar
attr_accessor :a1
def initialize
@a1='init'
end
end

var =":a1"

t = Testvar.new
if t.respond_to?("#{var}")
p 'test 1- pass'
end
if t.instance_variable_set var , 'abc'
p 'test 2-pass'
end
if t.send var , 'def'
p 'test 3-pass'
end
Try like this:

class Testvar
attr_accessor :a1
def initialize
@a1='init'
end
end

var = :a1

t = Testvar.new

if t.respond_to?(var)
puts 'test 1- pass'
end

t.instance_variable_set "@#{var}" , 'abc'
if t.instance_variable_get("@#{var}") == 'abc'
puts 'test 2-pass'
end

t.send "#{var}=" , 'def'
if t.send(var) == 'def'
puts 'test 3-pass'
end
 
R

Ralph Shnelvar

if t.respond_to?("#{var}")

Supernewbie question: Where is the documentation for respond_to? ?

Obviously ... I am just beginning to learn Ruby after trying to absorb
the Ruby on Rails and PickAxe books.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Supernewbie question: Where is the documentation for respond_to? ?

Obviously ... I am just beginning to learn Ruby after trying to absorb
the Ruby on Rails and PickAxe books.
For 1.8.6:
http://ruby-doc.org/core/

Then search for "respond_to?" and you will see three classes which define
it, in the frame on the far right, which lists methods. The three you will
see are:

respond_to? (DRb::DRbObject)<http://ruby-doc.org/core/classes/DRb/DRbObject.html#M007246>
respond_to? (Object) <http://ruby-doc.org/core/classes/Object.html#M000331>
respond_to? (Delegator)<http://ruby-doc.org/core/classes/Delegator.html#M002698>

Click the one for Object, and it should take you to
http://ruby-doc.org/core/classes/Object.html#M000331
 
B

Brian Candler

var =":a1"
t = Testvar.new
if t.respond_to?("#{var}")
p 'test 1- pass'
end

Here you are testing if your object has a particular method. The method
name is "a1". You can either pass the string "a1" or the symbol :a1

However you passed the string ":a1" consisting of three characters -
colon, a, 1.
if t.instance_variable_set var , 'abc'
p 'test 2-pass'
end

Here you are talking about an instance variable, not a method. The
instance variable's name is "@a1". You can either pass the string "@a1"
or the symbol :mad:a1
if t.send var , 'def'
p 'test 3-pass'
end

Here you're trying to call a setter method. The method's name is "a1=".
Again, you can either pass the string "a1=" or the symbol :a1=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top