$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 …
$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 …
On Tue, Dec 4, 2012 at 12:45 PM, Mallikarjuna Y.
[email protected] wrote:
My aim is to construct a global var ($rr_msg) and get the value …
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 04, 2012 at 09:00:56PM +0900, Jes
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs