What is wrong in this

$rr_msg = "hello"

task :default do
        display "rr"
end

def display(msg)
        ss = msg+"_msg"
        puts "#{$ss}"
end

My aim is to construct a global var ($rr_msg) and get the value ...

Can any one help me ......

···

--
Posted via http://www.ruby-forum.com/.

You can't construct a variable like that. In order to do this you need
to use eval:

1.9.2p290 :001 > $rr_msg = "test"
=> "test"
1.9.2p290 :002 > var = "rr"
=> "rr"
1.9.2p290 :004 > eval "$#{var}_msg"
=> "test"

But there's another way. Instead of constructing a variable, you could
create a hash whose keys are the "variables":

$messages = {"rr" => "test"} # not sure it needs to be global, but whatever

def display message_id
  puts $messages[message_id]
end

display "rr" #=> test

Jesus.

···

On Tue, Dec 4, 2012 at 12:45 PM, Mallikarjuna Yaddala <lists@ruby-forum.com> wrote:

$rr_msg = "hello"

task :default do
        display "rr"
end

def display(msg)
        ss = msg+"_msg"
        puts "#{$ss}"
end

My aim is to construct a global var ($rr_msg) and get the value ...

That's not just "another way" -- it's a much better way, for most
purposes. If you can avoid using eval without complicating your code
overmuch, you should, for purposes of not completely screwing up
something by (for instance) plugging user input directly into an eval and
ending up with horribleness being executed.

···

On Tue, Dec 04, 2012 at 09:00:56PM +0900, Jesús Gabriel y Galán wrote:

But there's another way. Instead of constructing a variable, you could
create a hash whose keys are the "variables":

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]