Jump to content

Category talk:Ruby: Difference between revisions

x = "string" creates a variable, or not.
No edit summary
(x = "string" creates a variable, or not.)
Line 49:
 
[[Special:Contributions/189.104.25.246|189.104.25.246]] 02:03, 10 July 2011 (UTC)
 
: With Ruby, <code>x = "string"</code> never creates a variable, unless there is no <code>x</code> in scope. I can check this with a closure:
 
: <lang ruby>def f(x)
x.replace("different string")
x << " from method"
$p = proc { x }
x = "another variable?"
end
 
x = "original string"
f(x)
puts x # => "different string from method"
puts $p.call # => "another variable?"</lang>
 
: Some other languages are simpler. With [[Common Lisp]], <code>(let ((x "string")) ...)</code> creates a variable and <code>(setq x "string")</code> sets it. With [[Factor]] (inside a <code>[let ... ]</code> block), <code>"string" :> x!</code> creates it and <code>"string" x!</code> sets it. With Ruby, <code>x = "string"</code> can either create it or set it. This is only important if some closure or binding captures <code>x</code>. --[[User:Kernigh|Kernigh]] 04:00, 11 August 2011 (UTC)
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.