M
Marcello Barnaba
How would you write the following statement:
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
?
Thanks![Smile :) :)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
?
Thanks
How would you write the following statement:
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
klass.send(ARGV[0] ? :up : :down)How would you write the following statement:
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
ARGV[0] ? klass.sendup) : klass.send
down)
Blessings,
TwP
:up => :upm=Hash.newdown).merge!({:up=>:up}) => {:up=>:up}
[ :up, :down, nil, :fred, "up", "down", "", "fred" ].each do |x| ?> puts "#{x.inspect} => #{m[(x.to_sym rescue :up)].inspect}"
end
How would you write the following statement:
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
How would you write the following statement:
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
The only idea I have so far is longer but less complex structured IMHO:
klass.send( case ARGV[0] when "up", nil then :up else :down end )
How would you write the following statement:
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
?
Thanks![]()
The other responders would call klass.down when ARGV[0] is false/
nil. Also you can't call nil.to_sym (as you probably learned from
ARGV[0].to_sym raising a NoMethodError) or "".to_sym (ArgumentError
for the empty string).
klass.send(Hash.newdown).merge!({:up=3D>:up})[(ARGV[0].to_sym =A0
rescue :up)])
Yuck! =A0Stick with the simple ternary for your statement (although you= =A0
might want to use the rescue trick instead of just ||).
klass.send(%w{up down}.findMarcello said:How would you write the following statement:
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
?
Marcello said:klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
klass.send(%w{up down}.findup) {|dir| dir == ARGV[0]})
The only way I can think of to get rid of the second 'up' is to
introduce an abstraction. It provides an additional piece of information
-- which one's the default. So:
dirs = %w{up down}
klass.send(dirs.find(dirs.first) {|dir| dir == ARGV[0] })
well, it turns out, not so clever. i got the api wrong - find expectsMarcello said:dirs = %w{up down}
klass.send(dirs.find(dirs.first) {|dir| dir == ARGV[0] })
clever use of .first (setting the default) and .find.
but I think that readability hit a regression.. and first we stood in front of
3 ups, now we stand in front of 4 dirs!![]()
How would you write the following statement:
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
?
Thanks![]()
How would you write the following statement:
klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down)
i would just do this
klass.send( ARGV[0] || 'up' )
and live with the exception, which will no less clear than
program.rb UP
sending the msg ':down' silently
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.