I have an array anArray = [ object_1, object_2, … object_n]
I would like to delete an object in it, if object.attribute_x =
anInteger
what is the dryest way to do it vs a C-style loop on each item of the
array ?
thanks fyh
joss
I have an array anArray = [ object_1, object_2, … object_n]
I would like to delete an object in it, if object.attribute_x =
anInteger
what is the dryest way to do it vs a C-style loop on each item of the
array ?
thanks fyh
joss
2007/8/17, Josselin [email protected]:
I have an array anArray = [ object_1, object_2, … object_n]
I would like to delete an object in it, if object.attribute_x = anInteger
what is the dryest way to do it vs a C-style loop on each item of the array ?
irb(main):001:0> %w{foo bar peter mountain}.delete_if {|s| s.length > 3}
=> [“foo”, “bar”]
robert
From: Josselin [mailto:[email protected]]
delete_if changes the array
irb(main):055:0> a=[“a”,1,“b”,2,3,“c”,4]
=> [“a”, 1, “b”, 2, 3, “c”, 4]
irb(main):056:0> a.delete_if{|x| x.is_a? Integer}
=> [“a”, “b”, “c”]
irb(main):057:0> a
=> [“a”, “b”, “c”]
reject creates another array copy
irb(main):061:0> a.reject{|x| x.is_a? Integer}
=> [“a”, “b”, “c”]
select is like reject but w reverse logic
irb(main):066:0> a.select{|x| not x.is_a? Integer}
=> [“a”, “b”, “c”]
kind regards -botp
On 2007-08-17 10:42:16 +0200, “Robert K.”
[email protected] said:
robert
thanks Robert… missed the delete_if { block } in reading my doc !
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs