[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

any way to write it in one line ?

Erwin

9/6/2008 6:17:00 PM

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
16 Answers

buddhamagnet

9/6/2008 6:20:00 PM

0

[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

On Sat, Sep 6, 2008 at 7:14 PM, Erwin <yves_dufour@mac.com> 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
>
>

Jesús Gabriel y Galán

9/6/2008 6:24:00 PM

0

On Sat, Sep 6, 2008 at 8:14 PM, Erwin <yves_dufour@mac.com> 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.

Jesús Gabriel y Galán

9/6/2008 6:37:00 PM

0

>
> On Sat, Sep 6, 2008 at 7:14 PM, Erwin <yves_dufour@mac.com> 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 Goodchild <buddhamagnet@gmail.com> 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.

Piyush Ranjan

9/6/2008 7:01:00 PM

0

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 <
jgabrielygalan@gmail.com> wrote:

> >
> > On Sat, Sep 6, 2008 at 7:14 PM, Erwin <yves_dufour@mac.com> wrote:
> >
> >> 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 ?
>
> On Sat, Sep 6, 2008 at 8:19 PM, Dave Goodchild <buddhamagnet@gmail.com>
> wrote:
> > 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.
>
>

Rico

9/6/2008 7:05:00 PM

0

On Sep 6, 7:17 pm, Erwin <yves_duf...@mac.com> 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 }

Rico

9/6/2008 7:13:00 PM

0

Oops, I mean:

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

William James

9/6/2008 7:16:00 PM

0

On Sep 6, 1:17 pm, Erwin <yves_duf...@mac.com> 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"]

David A. Black

9/6/2008 7:51:00 PM

0

Hi --

On Sun, 7 Sep 2008, William James wrote:

> On Sep 6, 1:17 pm, Erwin <yves_duf...@mac.com> wrote:
>> 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.r... for details and updates!

Robert Klemme

9/7/2008 8:38:00 AM

0

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

Jean-Michel

9/7/2008 2:50:00 PM

0

On Sep 6, 11:17 am, Erwin <yves_duf...@mac.com> 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