Any way to write it in one line?

works = Array.new
session[:cart_works].each_pair {|key, value| works << key if
value == user_id}

how to initialize the works array in the bloc ?

thanks for your lights

erwin

session[:cat_works].each_pair do |key, value| works = Array.new unless
works
works << key if value == user_id
end

On Sat, Sep 6, 2008 at 8:14 PM, Erwin [email protected] wrote:

works =  Array.new
session[:cart_works].each_pair {|key, value| works << key if

value == user_id}

how to initialize the works array in the bloc ?

Maybe:

works = session[:cart_works].inject([]) {|w, (k,v)| (v== user_id)? w +
[k]: w}

Jesus.

On Sat, Sep 6, 2008 at 7:14 PM, Erwin [email protected] wrote:

works =  Array.new
session[:cart_works].each_pair {|key, value| works << key if

value == user_id}

how to initialize the works array in the bloc ?

On Sat, Sep 6, 2008 at 8:19 PM, Dave G. [email protected]
wrote:

session[:cat_works].each_pair do |key, value| works = Array.new unless
works
works << key if value == user_id
end

I think that in this case works is not visible outside of the block
(unless you already had it defined, which kind of defeats the purpose):

irb(main):007:0> h.each_pair {|k,v| arr = Array.new unless arr; arr <<
k if v==3}
=> {:c=>3, :a=>3, :b=>5}
irb(main):008:0> arr
NameError: undefined local variable or method `arr’ for main:Object
from (irb):8

Jesus.

On Sep 6, 7:17 pm, Erwin [email protected] wrote:

 works =  Array.new
 session[:cart_works].each_pair {|key, value| works << key if

value == user_id}

how to initialize the works array in the bloc ?

thanks for your lights

erwin

works = session[:cart_works].keys.select { |key| session[:cart_works]
[:key] == user_id }

works = Array.new; session[:cart_works].each_pair {|key, value| works
<<
key if value == user_id}

there! fits in one line
:stuck_out_tongue: :stuck_out_tongue:

On Sun, Sep 7, 2008 at 12:06 AM, Jesús Gabriel y Galán <

On Sep 6, 1:17 pm, Erwin [email protected] wrote:

 works =  Array.new

works = []

Instead of

name = String.new

one says

name = “”

 session[:cart_works].each_pair {|key, value| works << key if

value == user_id}

how to initialize the works array in the bloc ?

thanks for your lights

erwin

h = Hash[ * %w(apple red lemon yellow bananna yellow orange orange) ]
==>{“bananna”=>“yellow”, “apple”=>“red”, “orange”=>“orange”,
“lemon”=>“yello
w”}
a = h.keys.select{|k| ‘yellow’==h[k] }
==>[“bananna”, “lemon”]

Oops, I mean:

works = session[:cart_works].keys.select { |key| session[:cart_works]
[key] == user_id }

Hi –

On Sun, 7 Sep 2008, William J. wrote:

name = “”

I tend to use the literal constructors, but either technique is OK. I
believe some people like to use Array.new consistently since sometimes
they actually need it (with arguments).

David

On 06.09.2008 20:17, Erwin wrote:

 works =  Array.new
 session[:cart_works].each_pair {|key, value| works << key if

value == user_id}

how to initialize the works array in the bloc ?

You cannot do it inside the block because then it is not know after the
block because of how the scoping works:

$ ruby -e ‘[1].each { w = 10 }; p w’
-e:1: undefined local variable or method `w’ for main:Object (NameError)

You can do

works = session[:cart_works].inject [] do |w,(k,v)|
w << k if v == user_id
w
end

works = session[:cart_works].select {|k,v| v == user_id}.map {|k,v| k}

Kind regards

robert

On Sep 6, 11:17 am, Erwin [email protected] wrote:

 works =  Array.new
 session[:cart_works].each_pair {|key, value| works << key if

value == user_id}

how to initialize the works array in the bloc ?

thanks for your lights

erwin

h = Hash[ * %w(apple red lemon yellow bananna yellow orange orange) ]
h.clone.delete_if { |k,v| v != ‘yellow’ }.keys

Jean-Michel

On Mon, Sep 8, 2008 at 8:18 AM, Michael F.
[email protected] wrote:

On Sun, Sep 7, 2008 at 3:14 AM, Erwin [email protected] wrote:

works =  Array.new
session[:cart_works].each_pair {|key, value| works << key if

value == user_id}

works = session[:cart_works].reject{|k,v| not v == user_id }.keys

Sorry, hit send too early ^^;

^ manveru

On 6 sep, 21:04, Rico [email protected] wrote:

erwin

works = session[:cart_works].keys.select { |key| session[:cart_works]
[:key] == user_id }

thanks a lot, all answers show exactly what I was looking for, …

On Sun, Sep 7, 2008 at 3:14 AM, Erwin [email protected] wrote:

works =  Array.new
session[:cart_works].each_pair {|key, value| works << key if

value == user_id}

works = session[:cart_works].reject{|k,v| v == user_id }.keys

On 6 sep, 21:16, William J. [email protected] wrote:

one says

erwin

h = Hash[ * %w(apple red lemon yellow bananna yellow orange orange) ]
==>{“bananna”=>“yellow”, “apple”=>“red”, “orange”=>“orange”,
“lemon”=>“yello
w”}
a = h.keys.select{|k| ‘yellow’==h[k] }
==>[“bananna”, “lemon”]

thanks a lot…

On 7 sep, 16:50, Jean-Michel [email protected] wrote:

erwin

h = Hash[ * %w(apple red lemon yellow bananna yellow orange orange) ]
h.clone.delete_if { |k,v| v != ‘yellow’ }.keys

Jean-Michel

thanks a lot …

On 7 sep, 10:37, Robert K. [email protected] wrote:

works = session[:cart_works].select {|k,v| v == user_id}.map {|k,v| k}

Kind regards

    robert

thanks Robert ! I have to look a little bit into the .select …