i++

hello,

my code in ruby looks like this…

···


i=0
arr_stuff.each {

a>
$tk_listbox_stuff.insert( i, a.to_s)
i += 1
}

I really love the c++ style with
++i
or
i++

Is there a smart way to do such things in ruby?

daniel

In the above example there’s an even smarter way:

arr_stuff.each_with_index { |a,i|
$tk_listbox_stuff.insert( i, a.to_s)
}

Neat, huh?

You can’t do “i++” or “i.succ!” because in Ruby numbers are immutable
objects, and variables always hold references to objects. When ‘i’ contains
a reference to the number 3, say, you can’t send a message to the number 3
saying “turn yourself into 4”.

All you can do is change ‘i’ to point to a new object, the number 4. The
only way you can change what a local variable points to is via assignment:

i = … some object …

Hence: i = (i+1), or i += 1 which expands to the same thing.

Regards,

Brian.

···

On Fri, Mar 28, 2003 at 06:54:58AM +0900, daniel wrote:

my code in ruby looks like this…

i=0
arr_stuff.each {

a>
$tk_listbox_stuff.insert( i, a.to_s)
i += 1
}

I really love the c++ style with
++i
or
i++

Is there a smart way to do such things in ruby?

daniel wrote:

my code in ruby looks like this…

i=0
arr_stuff.each {

a>
$tk_listbox_stuff.insert( i, a.to_s)
i += 1
}

I really love the c++ style with
++i
or
i++

Is there a smart way to do such things in ruby?

Did you just not like the answers that you got when you asked this
question yesterday? :wink:

not preinc or postinc, but

arr.size.times do |i|
$tk_listbox_stuff.insert i, a[i].to_s
end

or

arr.each_with_index do |a,i|
$tk_listbox_stuff.insert i, a.to_s
end

-a

···

On Thu, 27 Mar 2003, daniel wrote:

hello,

my code in ruby looks like this…

i=0
arr_stuff.each {

a>
$tk_listbox_stuff.insert( i, a.to_s)
i += 1
}

I really love the c++ style with
++i
or
i++

Is there a smart way to do such things in ruby?

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Avoiding the question of i++, a common idiom (if a google search for
“ruby tklistbox” is any indication) would be:

arr_stuff.each { |a|
$tk_listbox_stuff.insert(‘end’, a.to_s)
}

But even that seems unnecessarily verbose. According to the Perl/Tk
documentation (there doesn’t seem to be any Ruby/Tk documentation,
but Programming Ruby suggests Perl/Tk as an alternative) the #insert
method should be able to insert multiple items:

$tk_listbox_stuff.insert(‘end’, *arr_stuff)

Note that the above is untested (no Tk here) and that it also omits
an explicit #to_s on the array elements (a peek at tk.rb shows that
Ruby objects are automatically converted to Tcl strings).

···

“daniel” offstuff@aon.at wrote:

i=0
arr_stuff.each {

a>
$tk_listbox_stuff.insert( i, a.to_s)
i += 1
}


Tabby

Lyle Johnson lyle@users.sourceforge.net schrieb in im Newsbeitrag:
3E837D4B.2080306@users.sourceforge.net

Did you just not like the answers that you got when you asked this
question yesterday? :wink:

oh, yes - I like the answers I got yesterday.
I have wrote a new posting because I’m searching
for a solution like this or better.

···


i=0
arr.each {

a>
$tk_list.insert( i++, a.to_s )
}

The way I do this now in ruby is ugly.

daniel

Brian Candler B.Candler@pobox.com schrieb in im Newsbeitrag:
20030327220909.GA71406@uk.tiscali.com

···

On Fri, Mar 28, 2003 at 06:54:58AM +0900, daniel wrote:

my code in ruby looks like this…

i=0
arr_stuff.each {

a>
$tk_listbox_stuff.insert( i, a.to_s)
i += 1
}

I really love the c++ style with
++i
or
i++

Is there a smart way to do such things in ruby?

In the above example there’s an even smarter way:

arr_stuff.each_with_index { |a,i|
$tk_listbox_stuff.insert( i, a.to_s)
}

Neat, huh

thx for this answer! - its great!

Sabby and Tabby sabbyxtabby@yahoo.com schrieb in im Newsbeitrag:
f5a79bf2.0303280429.14bc682e@posting.google.com

···

“daniel” offstuff@aon.at wrote:

[…]

Avoiding the question of i++, a common idiom (if a google search for
“ruby tklistbox” is any indication) would be:

arr_stuff.each { |a|
$tk_listbox_stuff.insert(‘end’, a.to_s)
}

[…]

My code was only an example.
I’v searched for an ``alias´´ of the c++ code.

daniel

daniel offstuff@aon.at schrieb in im Newsbeitrag:
3e84b97e$0$20692$91cee783@newsreader02.highway.telekom.at…

Sabby and Tabby sabbyxtabby@yahoo.com schrieb in im Newsbeitrag:
f5a79bf2.0303280429.14bc682e@posting.google.com

[…]

Avoiding the question of i++, a common idiom (if a google search for
“ruby tklistbox” is any indication) would be:

arr_stuff.each { |a|
$tk_listbox_stuff.insert(‘end’, a.to_s)
}

[…]

additional…

$tk_listbox_stuff.insert(‘end’, a.to_s)
…is not a solve for my problem, because i need the idx for
extension things…

···

“daniel” offstuff@aon.at wrote: