Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
newbie ..need help with metaprogramming..
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dhaval  
View profile  
 More options Nov 12, 7:05 pm
Newsgroups: comp.lang.ruby
From: Dhaval <dhavalvtha...@gmail.com>
Date: Fri, 13 Nov 2009 09:05:07 +0900
Local: Thurs, Nov 12 2009 7:05 pm
Subject: newbie ..need help with metaprogramming..
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Caleb Clausen  
View profile  
 More options Nov 12, 7:42 pm
From: Caleb Clausen <vikk...@gmail.com>
Date: Fri, 13 Nov 2009 09:42:19 +0900
Local: Thurs, Nov 12 2009 7:42 pm
Subject: Re: newbie ..need help with metaprogramming..
On 11/12/09, Dhaval <dhavalvtha...@gmail.com> wrote:

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')


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Cheek  
View profile  
 More options Nov 12, 7:45 pm
From: Josh Cheek <josh.ch...@gmail.com>
Date: Fri, 13 Nov 2009 09:45:05 +0900
Local: Thurs, Nov 12 2009 7:45 pm
Subject: Re: newbie ..need help with metaprogramming..

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ralph Shnelvar  
View profile  
 More options Nov 13, 6:58 pm
From: Ralph Shnelvar <ral...@dos32.com>
Date: Sat, 14 Nov 2009 08:58:39 +0900
Local: Fri, Nov 13 2009 6:58 pm
Subject: Re: newbie ..need help with metaprogramming..

>> 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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Cheek  
View profile  
 More options Nov 13, 8:01 pm
From: Josh Cheek <josh.ch...@gmail.com>
Date: Sat, 14 Nov 2009 10:01:10 +0900
Local: Fri, Nov 13 2009 8:01 pm
Subject: Re: newbie ..need help with metaprogramming..

On Fri, Nov 13, 2009 at 5:58 PM, Ralph Shnelvar <ral...@dos32.com> wrote:
> >> 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.

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brian Candler  
View profile  
 More options Nov 16, 5:46 am
Newsgroups: comp.lang.ruby
From: Brian Candler <b.cand...@pobox.com>
Date: Mon, 16 Nov 2009 19:46:06 +0900
Local: Mon, Nov 16 2009 5:46 am
Subject: Re: newbie ..need help with metaprogramming..

> 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 :@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=
--
Posted via http://www.ruby-forum.com/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google