Instance/global variables

Trying to write a function foo that behaves as follows: foo(x), asks
for input and we give it ‘hello’, function creates a variable called x
and sets equal to ‘hello’. We now have an x variable with a value for
use at later time.

def foo(x)
x = gets.chomp!
return x
end

Now we call x and get “hello”

Not quite sure how to return that variable so that I can access it
later, any help?

i’m not on a machine with ruby installed at the moment, but i don’t see
why this wouldn’t work:

def foo
@x = gets.chomp!
end

the ‘return’ statement isn’t necessary, as ruby always returns the
last value evaluated for a given variable. the ‘@’ symbol creates an
instance variable, available anywhere within a class instance. ‘@@’
will give you a class variable, shared across all instances of a class.
‘$’ will give you a global variable, but you probably don’t really want
that - they’re ugly and muck up the works.

take a look here for a better explanation:
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Variables_and_Constants

  • j

jake kaiden wrote in post #1002759:

i’m not on a machine with ruby installed at the moment, but i don’t see
why this wouldn’t work:

def foo
@x = gets.chomp!
end

Say I try an example as follows:

foo(“bar”)

I try to call @bar, and nil is returned because that variable does not
exist.

If I create and assign @x before calling foo(x), then @x is assigned to
the input.
@x = " "
foo(“hello”)

@x
=>“hello”

I need a way to create and assign that variable and then pass it
up a scope and this is where I am confused.

Might not be pretty, but I think I’ve got it.

def foo(x)
eval("@{s2} = gets.chomp!")
end

Pretty much a function that creates an instance variable. Anyone know
of a better way of doing this?

Thanks.

jay s. wrote in post #1002782:

Anyone know
of a better way of doing this?

None of your posts make any sense. Let’s start with your first one:

===
def foo(x)
x = gets.chomp!
return x
end

Now we call x and get “hello”

Now we call x? What the heck are you talking about?

Your next post:

===

def foo
@x = gets.chomp!
end

Say I try an example as follows:

foo(“bar”)

foo() doesn’t accept arguments. Your comments and your code seem to
have no relation to each other.

hi jay,

sorry if i misunderstood what you wanted to do from your first post…

“…asks for input and we give it ‘hello’, function creates a variable
called x and sets equal to ‘hello’. We now have an x variable with a
value for use at later time.”

as 7stud pointed out, my suggestion doesn’t take an argument, but does
set the “x variable” to an instance variable with the value of the user
input:

def foo
@x = gets.chomp!
end

foo
puts @x

hello
=> hello

if you want to give #foo an argument, have that made an instance
variable, and be able to change the “x variable,” you could do something
like this:

def foo(x)
@x = x
end

@x = " "
puts @x

input = gets.chomp!
foo(input)
puts @x

foo(“bar”)
puts @x

your last post leads me to think that you don’t want the argument to
#foo set to the “x variable,” but that you want the argument itself to
become an instance variable. if that’s the case, then maybe something
like this is what you’re looking for:

input = gets.chomp! #input will have to start with “@”
instance_variable_set(input, “hello”)

or set both the variable and the value from user input:

irb(main):001:0> ivar = gets.chomp!
@bar
=> “@bar
irb(main):002:0> value = gets.chomp!
hello
=> “hello”
irb(main):003:0> instance_variable_set(ivar, value)
=> “hello”
irb(main):004:0> puts @bar
hello

any closer?

  • j

http://stackoverflow.com/questions/151338/adding-an-instance-variable-to-a-class-in-ruby

jay s. wrote in post #1003018:

This works for what I’m looking to do, just not sure if there is a
prettier way of doing it.

What if someone enters a string that is ruby code to erase your entire
hard drive?

your last post leads me to think that you don’t want the argument to
#foo set to the “x variable,” but that you want the argument itself to
become an instance variable. if that’s the case, then maybe something
like this is what you’re looking for:

input = gets.chomp! #input will have to start with “@”
instance_variable_set(input, “hello”)

or set both the variable and the value from user input:

irb(main):001:0> ivar = gets.chomp!
@bar
=> “@bar
irb(main):002:0> value = gets.chomp!
hello
=> “hello”
irb(main):003:0> instance_variable_set(ivar, value)
=> “hello”
irb(main):004:0> puts @bar
hello

any closer?

  • j

http://stackoverflow.com/questions/151338/adding-an-instance-variable-to-a-class-in-ruby

Sorry for not being clearer. I am looking to pass an argument and have
the function create an instance variable named after the argument passed
in and set to the value that the user inputs.

This works for what I’m looking to do, just not sure if there is a
prettier way of doing it.

def foo(x)
print “Please input value for variable: "
eval(”@#{x} = gets.chomp!")
end

foo(“string”)
=>Please input value for variable:

this is a string

puts @string
=>“this is a string”

Hope this is clearer

gotcha - i can be kinda thick at times…

seems like you found a nice solution - 2 lines look pretty “pretty” to
me :wink:

  • j

jay s. wrote in post #1003018:

What you should be doing is this:

def get_input(message)
print message
gets.chomp!
end

prompt = "Please input value for variable: "
input = get_input(prompt)

puts input

Methods should receive arguments and then return a result, which can
then be assigned to a variable of your choosing.

7stud – wrote in post #1003028:

What if someone enters a string that is ruby code to erase your entire
hard drive?

whoah there, good point… #eval might be heavy artillery for what
you want to do. i still lean towards #instance_value_set -
http://stackoverflow.com/questions/151338/adding-an-instance-variable-to-a-class-in-ruby

  • (somehow the name just seems right…) how about something like this?:

def foo(x)
print "enter value for variable: "
value = gets.chomp!
instvar = “@#{x}”
instance_variable_set(instvar, value)
end

foo(“string”)
p @string

enter value for variable: this is a string
=> “this is a string”

longer than two lines, and not as pretty - but probably a heck of a
lot safer…

cheers-

  • j

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