any way to write it in one line ?

E

Erwin

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
 
D

Dave Goodchild

[Note: parts of this message were removed to make it a legal post.]

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

Jesús Gabriel y Galán

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.
 
J

Jesús Gabriel y Galán

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 ?

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.
 
P

Piyush Ranjan

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

there! fits in one line
:p :p



On Sun, Sep 7, 2008 at 12:06 AM, Jes=FAs Gabriel y Gal=E1n <
works =3D Array.new
session[:cart_works].each_pair {|key, value| works << key if
value =3D=3D user_id}

how to initialize the works array in the bloc ?

session[:cat_works].each_pair do |key, value| works =3D Array.new unle= ss
works
works << key if value =3D=3D 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 =3D Array.new unless arr; arr <<
k if v=3D=3D3}
=3D> {:c=3D>3, :a=3D>3, :b=3D>5}
irb(main):008:0> arr
NameError: undefined local variable or method `arr' for main:Object
from (irb):8

Jesus.
 
R

Rico

     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 }
 
R

Rico

Oops, I mean:

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

William James

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"]
 
D

David A. Black

Hi --

works = Array.new

works = []

Instead of

name = String.new

one says

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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
R

Robert Klemme

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
 
J

Jean-Michel

     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
 
M

Michael Fellinger

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
 
E

Erwin

     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

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...
 
E

Erwin

     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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top