Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Simple iteration in a function problem

Reply
Thread Tools

Simple iteration in a function problem

 
 
Blake Miller
Guest
Posts: n/a
 
      12-01-2006
i'm trying to do this, and I'm stuck


class holderOfYs

def initialize()
@ys = Array.new
end

def addX( x )
@ys << x
end

# this is the problem function. all I want is the specific (ONE)
element
# from the @ys array that matches the id, but when you iterate over the
# array, i can't find a way to only have one element returned
def getXById(id)
@ys.each_with_index do |comp, index|
if( @ys[index].getUniqueId == id )
@ys.fetch(index)
end
end
end

class Y
@uniqueId

def getUniqueId
@uniqueId
end

end

class X < Y
def getUniqueId
super
end
end



I tried setting a variable like :
def getXById(id)
inst = X.new
@ys.each_with_index do |comp, index|
if( @ys[index].getUniqueId == id )
inst = @ys.fetch(index)
end
end

inst
end

...but I'm not looking to instantiate a new X, and even that solution
seems to screw up the @ys array.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      12-01-2006
Blake Miller wrote:
...
> # this is the problem function. all I want is the specific (ONE)
> element
> # from the @ys array that matches the id, but when you iterate over the
> # array, i can't find a way to only have one element returned
> def getXById(id)


Instead of:

> @ys.each_with_index do |comp, index|
> if( @ys[index].getUniqueId == id )
> @ys.fetch(index)
> end
> end


try this:

@ys.find {|x| x.getUniqueId == id}

> end



--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
 
 
 
Mike Fletcher
Guest
Posts: n/a
 
      12-01-2006
Blake Miller wrote:
> # this is the problem function. all I want is the specific (ONE)
> element
> # from the @ys array that matches the id, but when you iterate over the
> # array, i can't find a way to only have one element returned
> def getXById(id)
> @ys.each_with_index do |comp, index|
> if( @ys[index].getUniqueId == id )
> @ys.fetch(index)
> end
> end
> end


Perhaps

def getXById( id )
@ys.detect { |y| y.getUniqueId == id }
end

See the docs for Enumerable#detect about handling what's returned if
nothing is found.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      12-01-2006
On Dec 1, 2006, at 12:57 PM, Blake Miller wrote:

> i'm trying to do this, and I'm stuck


I've tried to answer your question and Rubyify your code a bit:

class YHolder
def initialize
@ys = Array.new
end

def add_y(y)
@ys << y
end
alias_method :<<, :add_y

def fetch_by_id(id)
@ys.find { |y| y.object_id == id }
end
alias_method :[], :fetch_by_id
end

class Y; end
class X < Y; end

holder, ids = YHolder.new, Array.new
10.times do
new_y_or_x = [Y, X][rand(2)].new

ids << new_y_or_x.object_id
holder << new_y_or_x
end

p holder # show the collection

# pick one by id
pick = ids[3]
p pick
p holder[pick]

# >> #<YHolder:0x1e2928 @ys=[#<Y:0x1e289c>, #<X:0x1e2874>, #<X:
0x1e284c>,
# >> #<Y:0x1e2824>, #<X:0x1e27fc>, #<Y:
0x1e27d4>,
# >> #<Y:0x1e27ac>, #<X:0x1e2784>, #<X:
0x1e275c>,
# >> #<Y:0x1e2734>]>
# >> 988178
# >> #<Y:0x1e2824>

__END__

Hope that helps.

James Edward Gray II

 
Reply With Quote
 
Blake Miller
Guest
Posts: n/a
 
      12-01-2006
Mike Fletcher wrote:
> Blake Miller wrote:
>> # this is the problem function. all I want is the specific (ONE)
>> element
>> # from the @ys array that matches the id, but when you iterate over the
>> # array, i can't find a way to only have one element returned
>> def getXById(id)
>> @ys.each_with_index do |comp, index|
>> if( @ys[index].getUniqueId == id )
>> @ys.fetch(index)
>> end
>> end
>> end

>
> Perhaps
>
> def getXById( id )
> @ys.detect { |y| y.getUniqueId == id }
> end
>
> See the docs for Enumerable#detect about handling what's returned if
> nothing is found.


I don't know where "detect" comes from (@ys is an array, and it doesn't
seem to be a method of Array)? however, I tried that and the result is
null, but I know for certain that the id being searched for does match
an element in the @ys array (because I wrote a function to print out the
ids in @ys)

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Blake Miller
Guest
Posts: n/a
 
      12-01-2006

>>
>> See the docs for Enumerable#detect about handling what's returned if
>> nothing is found.

>
> I don't know where "detect" comes from (@ys is an array, and it doesn't
> seem to be a method of Array)? however, I tried that and the result is
> null, but I know for certain that the id being searched for does match
> an element in the @ys array (because I wrote a function to print out the
> ids in @ys)


Sorry, missed that "Enumerable"

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Blake Miller
Guest
Posts: n/a
 
      12-01-2006

>
> # pick one by id
> pick = ids[3]
> p pick
> p holder[pick]
>
> # >> #<YHolder:0x1e2928 @ys=[#<Y:0x1e289c>, #<X:0x1e2874>, #<X:
> 0x1e284c>,
> # >> #<Y:0x1e2824>, #<X:0x1e27fc>, #<Y:
> 0x1e27d4>,
> # >> #<Y:0x1e27ac>, #<X:0x1e2784>, #<X:
> 0x1e275c>,
> # >> #<Y:0x1e2734>]>
> # >> 988178
> # >> #<Y:0x1e2824>
>
> __END__
>
> Hope that helps.
>
> James Edward Gray II


Note: The ids are strings. The @ys.find { |y| y.object_id == id } still
isn't working for me, i also tried y.object_id.equals(id) but I can't
seem to find the darn match : /

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Blake Miller
Guest
Posts: n/a
 
      12-01-2006

> I tried setting a variable like :
> def getXById(id)
> inst = X.new
> @ys.each_with_index do |comp, index|
> if( @ys[index].getUniqueId == id )
> inst = @ys.fetch(index)
> end
> end
>
> inst
> end
>
> ...but I'm not looking to instantiate a new X, and even that solution
> seems to screw up the @ys array.


I've also tried this:
def getXById(id)
matchingIndx = 0
@components.each_index do |index|
if( @components[index].getUniqueId == uniqueId )
matchingIndx = index
end
end

@components.fetch(matchingIndx)
end

but it just returns the 0th one every time (I'm assuming there wasn't a
match)

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Blake Miller
Guest
Posts: n/a
 
      12-01-2006
> Please show us your code, and make it as simple as possible. Like this:


class Widget

def initialize(title)
@components = Array.new
end

def add_component(component)
@components << component
end

def component(uniqueId) # note: uniqueId is a string
@components.find { |comp| comp.getUniqueId == uniqueId }
end
end

================================================== =======
class SearchWidgetComponent
:attr_reader :uniqueId

def getUniqueId
@uniqueId
end
end

=============================
class SearchWidgetComparisonComponent

def getUniqueId
super
end
end


in my view index.rhtml, I have:
<% @sw.component("swComparisonComp1").some_func_call_ on_swcc()... %>

I get an error " NoMethodError in Region#index" "You have a nil object
when you didn't expect it!" in index.rhtml at the
some_func_call_on_swcc() line

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Blake Miller
Guest
Posts: n/a
 
      12-01-2006
I think perhaps the problem is how I'm comparing strings. What is the
proper way to compare two strings, like:


@str1 = "boo"

def meth(str)
if( @str1 == str )
end

meth("boo")

Is that the appropriate way to compare them?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple array iteration not working? brian.haines@gmail.com Perl Misc 6 06-08-2009 04:25 AM
simple question re list iteration semantics Esmail Python 4 04-22-2009 03:11 AM
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
Simple python iteration question Bryan Python 14 08-15-2007 06:22 PM
macro function with iteration amit.man@gmail.com C Programming 14 03-08-2007 09:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57