Where is local variable declaration?

people, WTF?
if i use variable say “customer” and then use “customer” in subloop i’ll
be fucked up. is it so difficult to enable local variables, i cant
believe. you now playing worse than bad-minded PHP.

thank you, if you delete this topic i’ll recreate it

Vladimir K. wrote in post #1136072:

people, WTF?

Good day to you too, malchik.

if i use variable say “customer” and then use “customer” in subloop i’ll
be fucked up.

Wow, that sounds a bit extreme. I’ve never had that happen to me because
I made a mistake in a computer program. What looping constructs have
you misused in such a way? For example, here’s one that doesn’t have
any painful repercussions:

def foo *args
var = 1 # <-- local variable
args.each do |arg| # <-- loop
3.times do |i| # <-- subloop
var = (var + i) * arg
# <-- using variable in subloop
end
end
p var
end
foo 1, 2, 3 #=> 1281

is it so difficult to enable local variables, i cant
believe.

You keep using that term. I do not think it means what you think it
means.

def bar
b = 1 # <-- local variable
b
end
bar #=> 1

def baz
c = 1 # <-- local variable
lambda{|x| c = c + x } # <-- … used in a closure
end
baz[2] #=> 3

you now playing worse than bad-minded PHP.

Naughty, naughty bad PHP. You must spank her!

Out of interest, do you come from a javascript background?

thank you, if you delete this topic i’ll recreate it

You’re welcome. Why do you think we’d do that? Do you suspect that
some of your wording may have been unconstructive or offensive? Do you,
in fact, believe that your post was so offensive that we would
completely remove it from the forum? I only ask because you brought it
up; although I do have one question: why post something that you suspect
will be deleted, and even include in said post a description of what
would happen in the event of deletion, when you could instead alter the
post to be less offensive, and possibly even include some description of
your problem in a way that allows the great minds and friendly people in
this forum to offer you some constructive help/sympathy/etc.?

A loop does not define a new scope. So if you have defined a variable,
it will be available in the loop, you can modify it in the loop, and it
stays modified after the loop.

PHP works this way, and even Javascript.
Do you know a language that has loop level scope? I got interested.

ahh. i need to sorry for a first, i was too rude but you dont ban me,
heh,
and i appreciate it. sorry again. my point was:
def blabla
common_name = value
… some code
sub block
common_name = again # unintentially break variable value
end

end

so nobody including IDE can know i was rewritten outer variable.

i just want to way if you declare var inside block nothing dangerous
should happen. ruby borrows many from perl and in perl its ok. and yes,
i prefer to program javascript

Matthew K. wrote in post #1136074:

Vladimir K. wrote in post #1136072:

people, WTF?

Good day to you too, malchik.

if i use variable say “customer” and then use “customer” in subloop i’ll
be fucked up.

Wow, that sounds a bit extreme. I’ve never had that happen to me because
I made a mistake in a computer program. What looping constructs have
you misused in such a way? For example, here’s one that doesn’t have
any painful repercussions:

def foo *args
var = 1 # <-- local variable
args.each do |arg| # <-- loop
3.times do |i| # <-- subloop
var = (var + i) * arg
# <-- using variable in subloop
end
end
p var
end
foo 1, 2, 3 #=> 1281

is it so difficult to enable local variables, i cant
believe.

You keep using that term. I do not think it means what you think it
means.

def bar
b = 1 # <-- local variable
b
end
bar #=> 1

def baz
c = 1 # <-- local variable
lambda{|x| c = c + x } # <-- … used in a closure
end
baz[2] #=> 3

you now playing worse than bad-minded PHP.

Naughty, naughty bad PHP. You must spank her!

Out of interest, do you come from a javascript background?

thank you, if you delete this topic i’ll recreate it

You’re welcome. Why do you think we’d do that? Do you suspect that
some of your wording may have been unconstructive or offensive? Do you,
in fact, believe that your post was so offensive that we would
completely remove it from the forum? I only ask because you brought it
up; although I do have one question: why post something that you suspect
will be deleted, and even include in said post a description of what
would happen in the event of deletion, when you could instead alter the
post to be less offensive, and possibly even include some description of
your problem in a way that allows the great minds and friendly people in
this forum to offer you some constructive help/sympathy/etc.?

Vladimir K. wrote in post #1136087:

and im not from russia, im from Ukraine, so im not malchik, im hlopchik
:wink:

Vybachte

and im not from russia, im from Ukraine, so im not malchik, im hlopchik
:wink:

common_name = value
… some code
sub block
common_name = again # unintentially break variable value
end

Now we are talking :slight_smile:

Next question: what is “sub block”, that ends with “end”.
Can you paste some Ruby code?

Matthew K. wrote in post #1136088:

Vladimir K. wrote in post #1136087:

and im not from russia, im from Ukraine, so im not malchik, im hlopchik
:wink:

Vybachte

ahh so nice to hear ukrainian in foreign forum, thank you

On Sun, Feb 9, 2014 at 12:10 AM, Vladimir K. [email protected]
wrote:

end

so nobody including IDE can know i was rewritten outer variable.

First of all: no IDE in the world can know your intentions. Consider

def max(enum)
m = 0
enum.each {|i| m = i if i > m}
m
end

In this case it’s intentional that m is overwritten inside the block.
So there will be never an automated mechanism which will generally
warn you.

i just want to way if you declare var inside block nothing dangerous
should happen. ruby borrows many from perl and in perl its ok. and yes,
i prefer to program javascript

You can do this:

$ ruby -e ‘x=666;p x;2.times {|i,x=8| p [i,x];x=i}; p x’
666
[0, 8]
[1, 8]
666
$ ruby -e ‘x=666;p x;2.times {|i;x| p [i,x];x=i;p x}; p x’
666
[0, nil]
0
[1, nil]
1
666

Cheers

robert

I already found an answer to my question it seems

bad was:

Blocks define a new variable scope: variables created within a block exist only
within that block and are undefined outside of the block. Be cautious, however;
the local variables in a method are available to any blocks within that method. So
if a block assigns a value to a variable that is already defined outside of the
block, this does not create a new block-local variable but instead assigns a new
value to the already-existing variable.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

solution:

Ruby 1.9 is different in another important way, too. Block syntax has been
extended to allow you to declare block-local variables that are guaranteed to be
local, even if a variable by the same name already exists in the enclosing scope.
To do this, follow the list of block parameters with a semicolon and a
comma-separated list of block local variables.

so ok, thanks, problem is gone, it was just my laziness to read all the
docs heh, sorry.

bye, Vladimir

You can do this:

$ ruby -e ‘x=666;p x;2.times {|i,x=8| p [i,x];x=i}; p x’
666
[0, 8]
[1, 8]
666

Whoa, so the enumerator resets the variables to the defined value every
iteration? I didn’t know that (as I never used them).

On Tue, Feb 11, 2014 at 12:07 PM, Fldes L. [email protected]
wrote:

You can do this:

$ ruby -e ‘x=666;p x;2.times {|i,x=8| p [i,x];x=i}; p x’
666
[0, 8]
[1, 8]
666

Whoa, so the enumerator resets the variables to the defined value every
iteration? I didn’t know that (as I never used them).

That is common behavior for programming languages once a scope is
entered: every time the variable is reinitialized (even if it is just
nil that’s used).

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs