SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with syntax

    I have the following bit of code in a list method:
    Code:
    def new
    		if get_setting('app_type') == 'federal'
    			$parties = 'FederalParty'
    			$ed = 'FederalEd'
    		else
    			$parties = 'ProvParty'
    			$ed = 'ProvEd'
    		end
    		@edas = Eda.new
    		@parties = $parties.find_all
    		@eds = $ed.find_all
    	end
    How do I use the variables $parties and $ed to represent the model name to the right side of the expression. Can someone help me with this syntax?

    Rick
    Last edited by ricklach; Mar 28, 2006 at 23:22.
    Ruby, Ruby when will you be mine

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hope there is a better way to do this than what I am about to propose but I know this would work:

    Code:
    eval '@parties = ' + $parties + '.find_all'
    And the same for the rest... eval is just like eval() in PHP, it runs the code.
    And I have a question of my own, is it okay to just use $ in variables like that? I know there are varaiables in Ruby (system variables) like that such as $_ $` $1 $2 etc but is it okay to define one like that? I am just wondering because I've never done it.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am just a Ruby dabbler, but $ on a variable just makes it global in scope. You are fine to use them whenever you choose.

    @ricklach, Why store the string? Can't you just make them an object in the first place?

    $parties = FederalParty.new
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. There might be something wrong with the design of your code if you use globals

    2. Maybe this:

    Code:
    parties = FederalParty
    ...
    ...
    @parties = parties.new
    You store the class in the variable instead of the string.

    3. parties.new sounds a bit odd. Shouldn't that be party.new?

  5. #5
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all for that input. Your suggestions were indeed very useful. I had the same thought this morning about setting $parties=FederalParty, etc and since it would be used in other methods I would move it outside the current method and make it global so it can be accessed by other modules. I will give this a try and let you know how it turns out.

    Rick
    Ruby, Ruby when will you be mine

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    @parties = Kernel.const_get(get_setting('app_type').capitalize + "Party").find_all


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •