[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie question: how to create variable

Karel Michek

8/11/2006 4:53:00 PM

Hi,

how to create variable in the method that accepts string as name of the variable and the object?

Something like this:

class InterpreterCallback
def set_object(name, value)
#do something here
end
end

$callback = InterpreterCallback.new()
$callback.set_object("var", 3)

puts var

>>> 3

54 Answers

Robert Klemme

8/11/2006 7:54:00 PM

0

Karel Michek wrote:
> Hi,
>
> how to create variable in the method that accepts string as name of the variable and the object?
>
> Something like this:
>
> class InterpreterCallback
> def set_object(name, value)
> #do something here

instance_variable_set("@#{name}", value)
> end
> end
>
> $callback = InterpreterCallback.new()
> $callback.set_object("var", 3)
>
> puts var
>
>>>> 3
>

Alternatively you can use an OpenStruct.

Kind regards

robert

dblack

8/12/2006 12:27:00 PM

0

Karel Michek

8/13/2006 5:36:00 AM

0


> ------------ Puvodní zpráva ------------
> Od: <dblack@wobblini.net>
> Predmet: Re: newbie question: how to create variable
> Datum: 12.8.2006 14:27:31
> ----------------------------------------
> Hi --
>
> On Sat, 12 Aug 2006, Mr Pinto wrote:
>
> > On Aug 11, 2006, at 12:55 PM, Robert Klemme wrote:
> >
> >> Karel Michek wrote:
> >>> Hi,
> >>> how to create variable in the method that accepts string as name of the
> >>> variable and the object? Something like this:
> >>> class InterpreterCallback
> >>> def set_object(name, value)
> >>> #do something here
> >>
> >> instance_variable_set("@#{name}", value)
> >>> end
> >>> end
> >>> $callback = InterpreterCallback.new()
> >>> $callback.set_object("var", 3)
> >>> puts var
> >>>>>> 3
> >
> > Depending on what you're trying to do, you might be able to use a simple eval
>
> > statement:
> > irb(main):001:0> name="var"
> > => "var"
> > irb(main):002:0> value="3"
> > => "3"
> > irb(main):003:0> eval "#{name} = value"
> > => "3"
> > irb(main):004:0> var
> > => "3"
>
> That only works in irb. With ruby you'll get:
>
> ruby -e 'name="var"; value="3"; eval("#{name}=#{value}"); var'
> -e:1: undefined local variable or method `var' for main:Object
> (NameError)
>
>
> David
>
>

None of this actually works quite well for me. I am using SWIG directors to embed ruby (and python, perl) into C# application trough C++ layer value actually wraps C++ object . InterpreterCallback actually extends c++ class and I am calling set_object() from C++ - set_object("editor", getEditor() ) - "editor" then can be used in the global namespace for scripting convenience:

editor.DrawGrid

I actually get it working with

def set_object(var, value)
eval("def #{var}; $#{var}; end; proc{|_v| $#{var} = _v}", TOPLEVEL_BINDING).call(value)
end

but it goes through global variable. I believe there is simpler way how to do this.
Kind regards,
Karel

John

4/25/2009 10:00:00 AM

0

"Sea Wasp (Ryk E. Spoor)" <seawasp@sgeinc.invalid.com> wrote:

>nobody wrote:
>> "Sea Wasp (Ryk E. Spoor)" <seawasp@sgeinc.invalid.com> wrote:
>>
>>> For the third time in a row, I am participating in International
>>> Pixel-Stained Technopeasant Wretch day; I have posted a (reasonably
>>> stand-alone) section of one of my current works-in-progress, the novel
>>> "Fall of Saints", part 1 of the projected Balanced Sword fantasy
>>> trilogy.
>>
>> ("bullet-sized"? They have bullets? I'll have to wait for the
>> rest...)
>
> "bullets" pre-dated guns AFAIK. Used for slings.

I never realized the rocks I was slinging should've been called
bullets (even though they were golf-ball size or larger). Did they
really call them that?

Hmmm, wikipedia says "Originally, bullets were metallic or stone balls
used in the 'Sling' or 'Slingshot' as a weapon and for hunting."

For a slingshot (the rubber band jobbies) the size is about right, a
bit on the large side maybe. For a sling (swing and let go) they'd be
way too small. Anyway there is really no common load for the two
weapons, they're very different. And the slingshot doesn't predate
rubber bands <g>.

Whatever, not trying to bust your chops.

>> I like little Duckweed. Classy guy.
>>
>
> He thanks you. :) (Character in my RPGs, currently with a party going
>through Ravenloft.)

The piece I read seemed much more Tolkien-esque than Digital Knight,
is that a direction you're heading or just what was right for the new
piece?

I've noticed that rasfc seems more oriented towards Fantasy than
Sci-Fi or speculative fiction, but I'm not sure if that's because of
an actual orientation/trend or because the rasfc stream is currently
passing over fantasy rocks.

--
http://fictionfromnobody.bl...

spam

4/25/2009 12:23:00 PM

0

On Sat, 25 Apr 2009 03:59:38 -0600, nobody <no@spam.please> wrote:

>"Sea Wasp (Ryk E. Spoor)" <seawasp@sgeinc.invalid.com> wrote:
>
>>nobody wrote:
>>> "Sea Wasp (Ryk E. Spoor)" <seawasp@sgeinc.invalid.com> wrote:
>>>
>>>> For the third time in a row, I am participating in International
>>>> Pixel-Stained Technopeasant Wretch day; I have posted a (reasonably
>>>> stand-alone) section of one of my current works-in-progress, the novel
>>>> "Fall of Saints", part 1 of the projected Balanced Sword fantasy
>>>> trilogy.
>>>
>>> ("bullet-sized"? They have bullets? I'll have to wait for the
>>> rest...)
>>
>> "bullets" pre-dated guns AFAIK. Used for slings.
>
>I never realized the rocks I was slinging should've been called
>bullets (even though they were golf-ball size or larger). Did they
>really call them that?
>
>Hmmm, wikipedia says "Originally, bullets were metallic or stone balls
>used in the 'Sling' or 'Slingshot' as a weapon and for hunting."

A bullet is a little ball. You'd think a ballet would be a little ball,
or two people dancing in a cupboard, but there you go.

>I've noticed that rasfc seems more oriented towards Fantasy than
>Sci-Fi or speculative fiction, but I'm not sure if that's because of
>an actual orientation/trend or because the rasfc stream is currently
>passing over fantasy rocks.

We generally recommend that someone with a "hard science" question ask
it in r.a.sf.science -- not because we don't like it, but because there
are people there who can probably give more informed answers.

Zeborah was mainly writing SF, rather than fantasy, but she has left the
group. A few others have talked about their SF, too. If you stay around
long enough for us to get back to discussing writing, and if that
happens before they leave too, you'll no doubt see posts from them.

There have been plenty of writing-related questions, in the past, from
people working on SF of varying degrees of hardness, but recently (i.e.
for the last few years) most of the posts have been discussing the
future of rasfc, and how we can get back to discussing writing. These
meta posts have drowned out the writing posts. OTOH, they were
considered preferable to all the politics/ideology posts that preceded
them.

Just my perception of it, or two cents worth, if I had two cents.

Jonathan

--
"If common sense were a reliable guide, we wouldn't need
science in the first place." Amanda Gefter, New Scientist.

John

4/25/2009 1:30:00 PM

0

spam@sofluc.co.uk.invalid (Jonathan L Cunningham) wrote:

>...meta posts have drowned out the writing posts. OTOH, they were
>considered preferable to all the politics/ideology posts that preceded...

That seems to be an inherent problem with writing newsgroups. As far
as I can tell the only solution is to drown out the drivel with
on-topic posts.

--
FAQ POINTER: http://www.lshelby.com/ras...
my fiction: http://fictionfromnobody.bl...

Nicky

4/25/2009 2:20:00 PM

0

On Apr 25, 2:29 pm, nobody <n...@spam.please> wrote:
> s...@sofluc.co.uk.invalid (Jonathan L Cunningham) wrote:
>
> >...meta posts have drowned out the writing posts. OTOH, they were
> >considered preferable to all the politics/ideology posts that preceded...
>
> That seems to be an inherent problem with writing newsgroups.  As far
> as I can tell the only solution is to drown out the drivel with
> on-topic posts.
>
Love, you are the drivel.


John

4/25/2009 2:33:00 PM

0

Nicky <nicky.matthews@btinternet.com> wrote:

>On Apr 25, 2:29 pm, nobody <n...@spam.please> wrote:
>> s...@sofluc.co.uk.invalid (Jonathan L Cunningham) wrote:
>>
>> >...meta posts have drowned out the writing posts. OTOH, they were
>> >considered preferable to all the politics/ideology posts that preceded...
>>
>> That seems to be an inherent problem with writing newsgroups.  As far
>> as I can tell the only solution is to drown out the drivel with
>> on-topic posts.
>>
>Love, you are the drivel.

Consider this post from Nicky. Is it on-topic or is it meta-whining?

--
FAQ POINTER: http://www.lshelby.com/ras...
my fiction: http://fictionfromnobody.bl...

Sea Wasp (Ryk E. Spoor)

4/25/2009 3:35:00 PM

0

nobody wrote:
> "Sea Wasp (Ryk E. Spoor)" <seawasp@sgeinc.invalid.com> wrote:
>
>> nobody wrote:
>>> "Sea Wasp (Ryk E. Spoor)" <seawasp@sgeinc.invalid.com> wrote:
>>>
>>>> For the third time in a row, I am participating in International
>>>> Pixel-Stained Technopeasant Wretch day; I have posted a (reasonably
>>>> stand-alone) section of one of my current works-in-progress, the novel
>>>> "Fall of Saints", part 1 of the projected Balanced Sword fantasy
>>>> trilogy.
>>> ("bullet-sized"? They have bullets? I'll have to wait for the
>>> rest...)
>> "bullets" pre-dated guns AFAIK. Used for slings.
>
> I never realized the rocks I was slinging should've been called
> bullets (even though they were golf-ball size or larger). Did they
> really call them that?
>
> Hmmm, wikipedia says "Originally, bullets were metallic or stone balls
> used in the 'Sling' or 'Slingshot' as a weapon and for hunting."

And there you go! :)


>>> I like little Duckweed. Classy guy.
>>>
>> He thanks you. :) (Character in my RPGs, currently with a party going
>> through Ravenloft.)
>
> The piece I read seemed much more Tolkien-esque than Digital Knight,
> is that a direction you're heading or just what was right for the new
> piece?

"Names" is a small section (chapters 7, 8, and 9) of "Fall of Saints",
part of a fantasy trilogy currently called "The Balanced Sword".

It does take place in the same universe as Digital Knight, but it's on
a different world, Zarathan (which is mentioned in DK once, by Eonae
talking to Verne/Vierna). The actions in The Balanced Sword and in my
other projected trilogy, Spirit Warriors, eventually have an effect on
Jason's life. In a sense, by the time of "Shadow of Fear", they already
have.


>
> I've noticed that rasfc seems more oriented towards Fantasy than
> Sci-Fi or speculative fiction, but I'm not sure if that's because of
> an actual orientation/trend or because the rasfc stream is currently
> passing over fantasy rocks.
>

Fantasy outsells SF by at least 2:1 and perhaps by as much as 10:1.


--
Sea Wasp
/^ ;;;
Live Journal: http://seawasp.livej...

John

4/25/2009 3:43:00 PM

0

"Sea Wasp (Ryk E. Spoor)" <seawasp@sgeinc.invalid.com> wrote:

>nobody wrote:

>> I've noticed that rasfc seems more oriented towards Fantasy than
>> Sci-Fi or speculative fiction, but I'm not sure if that's because of
>> an actual orientation/trend or because the rasfc stream is currently
>> passing over fantasy rocks.
>>
>
> Fantasy outsells SF by at least 2:1 and perhaps by as much as 10:1.

Any idea why?

--
FAQ POINTER: http://www.lshelby.com/ras...
my fiction: http://fictionfromnobody.bl...