Mailinglist Archive: yast-devel (144 mails)

< Previous Next >
Re: [yast-devel] Questions about some YCP Equivalents in ruby
On 08/13/2013 01:30 PM, Thomas Fehr wrote:
Hi,

While I was just fixing a bug in current head and had to test the
fix anyway I assumed I could easily modify the ruby code needing
to be adapted anyway to a more ruby-like style.

I replaced the common usage of hash access with default parameter by

YCP Code --> Ruby like replacement
part["fsid"]:0 --> (part["fsid"]||0)
part["type"]:`none --> (part["type"]||:none)

This might cause a buggy behavior if the value was /false/ !

part["format"] = false
part.fetch("format", true) --> returns false
part["format"] || true --> returns true

irb(main):001:0> part = {}
=> {}
irb(main):002:0> part["format"] = false
=> false
irb(main):003:0> part.fetch("format", true)
=> false
irb(main):004:0> part["format"] || true
=> true

I've found some nice article on this:
http://www.dotnetguy.co.uk/post/2012/08/19/ruby-hash-fetch-returning-a-value-for-a-key-that-does-not-exist/

substring(part["device"]:"",0,9) --> part["device"][0,9]

I haven't checked that but you can access string as it was an array, so it seems this might work well.

irb(main):001:0> a = "12345678901234567890"
=> "12345678901234567890"
irb(main):002:0> a[0,9]
=> "123456789"
irb(main):003:0> a[0,2]
=> "12"
irb(main):004:0> a[2,4]
=> "3456"

Looks good.

Could someone with more ruby experience tell me if these replacements
are reasonable? My testing showed they work, but I am not sure if there
are pitfalls I am not aware of.

HTH
Lukas

--

Lukas Ocilka, Cloud & Systems Management Department
SUSE LINUX s.r.o., Praha
--
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: yast-devel+owner@xxxxxxxxxxxx

< Previous Next >
Follow Ups
References