Module: Yast::Builtins

Extended by:
I18n
Defined in:
src/ruby/yast/builtins.rb

Overview

Note:

All builtins return copy of result

Contains builtins available in YCP for easier transition. Big part of methods are deprecated.

For logging use Logger module instead of deprecated Builtins.y2milestone,... functions

Defined Under Namespace

Modules: Float, List, Multiset

Constant Summary

Constants included from I18n

I18n::DEFAULT_LOCALE

Class Method Summary collapse

Methods included from I18n

N_, Nn_, _, n_, textdomain

Class Method Details

.add(object, *params) ⇒ Object

Deprecated.

Use ruby operators for it.

Adds element to copy of element and return such copy.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'src/ruby/yast/builtins.rb', line 24

def self.add(object, *params)
  case object
  when ::Array then return Yast.deep_copy(object).concat(Yast.deep_copy(params))
  when ::Hash then  return Yast.deep_copy(object).merge(Yast.deep_copy(::Hash[*params]))
  when Yast::Path then return object + params.first
  when Yast::Term then
    res = Yast.deep_copy(object)
    res.params << Yast.deep_copy(params.first)
    return res
  when ::NilClass then return nil
  else
    raise "Invalid object '#{object.inspect}' for add builtin"
  end
end

.argsof(term) ⇒ Object

Deprecated.

use Term#params instead

Returns the arguments of a term.



1063
1064
1065
1066
1067
# File 'src/ruby/yast/builtins.rb', line 1063

def self.argsof(term)
  return nil if term.nil?

  Yast.deep_copy(term.params)
end

.change(object, *params) ⇒ Object

Deprecated.

use ruby native methods

  • Changes a list. Deprecated, use LIST[size(LIST)] = value. - change(, )
  • Change element pair in a map. Deprecated, use MAP[KEY] = VALUE. - change(, , ) it's obsoleted, behaves like add() builtin now


43
44
45
# File 'src/ruby/yast/builtins.rb', line 43

def self.change(object, *params)
  add object, *params
end

.contains(list, value) ⇒ Object

Deprecated.

Use Array#include?

contains() Yast built-in Checks if a list contains an element



337
338
339
340
# File 'src/ruby/yast/builtins.rb', line 337

def self.contains(list, value)
  return nil if list.nil? || value.nil?
  list.include? value
end

.deletechars(string, chars) ⇒ Object

Deprecated.

use ruby native method for string handling like String#gsub or String#delete

Removes all characters from a string



746
747
748
749
750
# File 'src/ruby/yast/builtins.rb', line 746

def self.deletechars(string, chars)
  return nil if !string || !chars

  string.gsub(/[#{Regexp.escape chars}]/, "")
end

.dgettext(domain, text) ⇒ Object

Translates the text using the given text domain



754
755
756
757
758
759
760
761
# File 'src/ruby/yast/builtins.rb', line 754

def self.dgettext(domain, text)
  old_text_domain = FastGettext.text_domain
  textdomain domain
  return _(text)
ensure
  FastGettext.text_domain = old_text_domain
  textdomain old_text_domain
end

.dngettext(domain, singular, plural, num) ⇒ Object

Translates the text using a locale-aware plural form handling



764
765
766
767
768
769
770
771
# File 'src/ruby/yast/builtins.rb', line 764

def self.dngettext(domain, singular, plural, num)
  old_text_domain = FastGettext.text_domain
  textdomain domain
  return n_(singular, plural, num)
ensure
  FastGettext.text_domain = old_text_domain
  textdomain old_text_domain
end

.dpgettext(domain, dirname, text) ⇒ Object

Translates the text using the given text domain and path



774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'src/ruby/yast/builtins.rb', line 774

def self.dpgettext(domain, dirname, text)
  old_text_domain = FastGettext.text_domain

  # remember the domain => file mapping, the same domain might be
  # used from a different path, then we need to reload the translations
  @textdomain_mapping ||= {}

  # check if the domain is already loaded from the path
  if @textdomain_mapping[domain] != dirname && !FastGettext.translation_repositories[domain]
    FastGettext.add_text_domain(domain, path: dirname)
    @textdomain_mapping[domain.dup] = dirname.dup
  end
  FastGettext.text_domain = domain
  return Translation._(text)
ensure
  FastGettext.text_domain = old_text_domain
end

.eval(object) ⇒ Object

Deprecated.

for lazy evaluation use builtin lambda or block calls

Evaluate a Yast value.



545
546
547
# File 'src/ruby/yast/builtins.rb', line 545

def self.eval(object)
  object.respond_to?(:call) ? object.call : Yast.deep_copy(object)
end

.filter(object, &block) ⇒ Object

Deprecated.

use ruby native select method

  • Filters a List
  • Filter a Map


50
51
52
53
# File 'src/ruby/yast/builtins.rb', line 50

def self.filter(object, &block)
  # TODO: investigate break and continue with filter as traverse workflow is different for ruby
  (object.is_a?(::Array) || object.is_a?(::Hash)) ? Yast.deep_copy(object).select(&block) : nil
end

.filterchars(string, chars) ⇒ Object

Deprecated.

use ruby native method for string handling like String#gsub or String#delete

Filters characters out of a ::String



794
795
796
797
798
# File 'src/ruby/yast/builtins.rb', line 794

def self.filterchars(string, chars)
  return nil if string.nil? || chars.nil?

  string.gsub(/[^#{Regexp.escape chars}]/, "")
end

.find(object, what = nil, &block) ⇒ Object

Deprecated.

use native ruby method find

find() Yast built-in

  • Returns position of a substring (-1 if not found)
  • Searches for the first occurence of a certain element in a list


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'src/ruby/yast/builtins.rb', line 59

def self.find(object, what = nil, &block)
  return nil if object.nil? || (what.nil? && block.nil?)

  case object
  when ::String
    ret = object.index what
    return ret.nil? ? -1 : Yast.deep_copy(ret)
  when ::Array
    Yast.deep_copy(object.find(&block))
  else
    raise "Invalid object for find() builtin"
  end
end

.findfirstnotof(string, chars) ⇒ Object

Deprecated.

use String#index instead

Searches string for the first non matching chars



802
803
804
805
806
# File 'src/ruby/yast/builtins.rb', line 802

def self.findfirstnotof(string, chars)
  return nil if string.nil? || chars.nil?

  string.index(/[^#{Regexp.escape chars}]/)
end

.findfirstof(string, chars) ⇒ Object

Deprecated.

use String#index instead

Finds position of the first matching characters in string



810
811
812
813
814
# File 'src/ruby/yast/builtins.rb', line 810

def self.findfirstof(string, chars)
  return nil if string.nil? || chars.nil?

  string.index(/[#{Regexp.escape chars}]/)
end

.findlastnotof(string, chars) ⇒ Object

Deprecated.

use String#rindex instead

Searches the last element of string that doesn't match



818
819
820
821
822
# File 'src/ruby/yast/builtins.rb', line 818

def self.findlastnotof(string, chars)
  return nil if string.nil? || chars.nil?

  string.rindex(/[^#{Regexp.escape chars}]/)
end

.findlastof(string, chars) ⇒ Object

Deprecated.

use String#rindex instead

Searches string for the last match



826
827
828
829
830
# File 'src/ruby/yast/builtins.rb', line 826

def self.findlastof(string, chars)
  return nil if string.nil? || chars.nil?

  string.rindex(/[#{Regexp.escape chars}]/)
end

.flatten(value) ⇒ Object

Deprecated.

Use Array#flatten but be aware different behavior for nil in Array

Flattens List



344
345
346
347
348
349
350
351
# File 'src/ruby/yast/builtins.rb', line 344

def self.flatten(value)
  return nil if value.nil?

  value.reduce([]) do |acc, i|
    return nil if i.nil?
    acc.concat(Yast.deep_copy(i))
  end
end

.foreach(object, &block) ⇒ Object

Deprecated.

use ruby native each method

  • Process the content of a map
  • Processes the content of a list


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'src/ruby/yast/builtins.rb', line 76

def self.foreach(object, &block)
  res = nil
  object = Yast.deep_copy(object)
  if object.is_a? ::Array
    begin
      object.each do |i|
        res = block.call(i)
      end
    rescue Yast::Break
      res = nil
    end
  elsif object.is_a? ::Hash
    begin
      # sort keys so it behaves same as in Yast
      sort(object.keys).each do |k|
        res = block.call(k, object[k])
      end
    rescue Yast::Break
      res = nil
    end
  else
    Yast.y2warning(1, "foreach builtin called on wrong type #{object.class}")
  end
  res
end

.getenv(value) ⇒ Object

Deprecated.

use ENV#[]

Change or add an environment variable



551
552
553
# File 'src/ruby/yast/builtins.rb', line 551

def self.getenv(value)
  ENV[value]
end

.haskey(map, key) ⇒ Object

Deprecated.

use Hash#haskey?

Check if map has a certain key



500
501
502
503
# File 'src/ruby/yast/builtins.rb', line 500

def self.haskey(map, key)
  return nil if map.nil? || key.nil?
  map.key? key
end

.isempty(object) ⇒ Object

Deprecated.

use native empty? method

  • Returns whether the map m is empty.
  • Returns whether the string s is empty.
  • Returns whether the list l is empty.


106
107
108
109
# File 'src/ruby/yast/builtins.rb', line 106

def self.isempty(object)
  return nil if object.nil?
  object.empty?
end

.issubstring(string, substring) ⇒ Object

Deprecated.

use String#include? instead

issubstring() Yast built-in searches for a specific string within another string



835
836
837
838
# File 'src/ruby/yast/builtins.rb', line 835

def self.issubstring(string, substring)
  return nil if string.nil? || substring.nil?
  string.include? substring
end

.listmap(list, &block) ⇒ Object

Deprecated.

for mapping of list to hash use various ruby builtins like Hash.[] or Enumerable#reduce

Maps an operation onto all elements of a list and thus creates a map.



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'src/ruby/yast/builtins.rb', line 387

def self.listmap(list, &block)
  return nil if list.nil?

  res = ::Hash.new
  begin
    Yast.deep_copy(list).each do |i|
      res.merge! block.call(i)
    end
  rescue Yast::Break
    # break stops adding to hash
    Yast.y2debug(1, "break in listmap")
  end

  res
end

.lookup(map, key, default) ⇒ Object

Deprecated.

Select a map element (deprecated, use MAP[KEY]:DEFAULT)



507
508
509
# File 'src/ruby/yast/builtins.rb', line 507

def self.lookup(map, key, default)
  map.key?(key) ? Yast.deep_copy(map[key]) : Yast.deep_copy(default)
end

.lsort(list) ⇒ Object

Deprecated.

use Array#sort but be aware differences between ruby and old ycp sorting

Sort A List respecting locale

See Also:

  • for details how it sorts


406
407
408
409
410
# File 'src/ruby/yast/builtins.rb', line 406

def self.lsort(list)
  return nil if list.nil?

  Yast.deep_copy(list.sort { |s1, s2| Ops.comparable_object(s1, true) <=> s2 })
end

.lsubstring(string, offset, length = -1)) ⇒ Object

Deprecated.

use String#include? instead

Extracts a substring in UTF-8 encoded string



842
843
844
845
# File 'src/ruby/yast/builtins.rb', line 842

def self.lsubstring(string, offset, length = -1)
  # ruby2.0 use by default UTF-8.
  substring string, offset, length
end

.maplist(object, &block) ⇒ Object

Deprecated.

use ruby native method Enumerable#map

  • Maps an operation onto all elements key/value and create a list
  • Maps an operation onto all elements of a list and thus creates a new list.


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'src/ruby/yast/builtins.rb', line 114

def self.maplist(object, &block)
  case object
  when ::Array
    res = []
    begin
      object.each do |i|
        res << block.call(Yast.deep_copy(i))
      end
    rescue Yast::Break
      # break skips out of each loop, but allow to keep previous results
      Yast.y2debug(1, "break in maplist(Array)")
    end
    return res
  when ::Hash
    res = []
    begin
      sort(object.keys).each do |k|
        res << block.call(Yast.deep_copy(k), Yast.deep_copy(object[k]))
      end
    rescue Yast::Break
      # break skips out of each loop, but allow to keep previous results
      Yast.y2debug(1, "break in maplist(Hash)")
    end
    return res
  else
    Yast.y2warning(1, "maplist builtin called on wrong type #{object.class}")
    return nil
  end
end

.mapmap(map, &block) ⇒ Object

Deprecated.

use ruby native methods for creating new Hash from other Hash

Maps an operation onto all key/value pairs of a map



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'src/ruby/yast/builtins.rb', line 513

def self.mapmap(map, &block)
  return nil if map.nil?
  unless map.is_a?(Hash)
    raise TypeError, "expected a Hash, got a #{map.class}"
  end

  map = Yast.deep_copy(map)
  res = ::Hash.new
  begin
    sort(map.keys).each do |k|
      res.merge! block.call(k, map[k])
    end
  rescue Yast::Break
    # break stops adding to hash
    Yast.y2debug(1, "break in mapmap")
  end

  res
end

.merge(a1, a2) ⇒ Object

Deprecated.

use Array#+

merge() Yast built-in Merges two lists into one



415
416
417
418
# File 'src/ruby/yast/builtins.rb', line 415

def self.merge(a1, a2)
  return nil if a1.nil? || a2.nil?
  Yast.deep_copy(a1 + a2)
end

.mergestring(string, sep) ⇒ Object

Deprecated.

use String#join instead

mergestring() Yast built-in Joins list elements with a string



850
851
852
853
854
# File 'src/ruby/yast/builtins.rb', line 850

def self.mergestring(string, sep)
  return nil if string.nil? || sep.nil?

  string.join sep
end

.prepend(list, element) ⇒ Object

Deprecated.

use Array#unshift

Prepends a list with a new element



422
423
424
425
426
# File 'src/ruby/yast/builtins.rb', line 422

def self.prepend(list, element)
  return nil if list.nil?

  [Yast.deep_copy(element)].concat(Yast.deep_copy(list))
end

.random(max) ⇒ Object

Deprecated.

use Kernel#rand

Random number generator.



557
558
559
560
561
# File 'src/ruby/yast/builtins.rb', line 557

def self.random(max)
  return nil if max.nil?

  max < 0 ? -rand(max) : rand(max)
end

.regexpmatch(string, pattern) ⇒ Boolean?

Deprecated.

use string =~ pattern

Searches a string for a POSIX Extended Regular Expression match.

If string or pattern is nil, or if pattern is an invalid regex, nil is returned.

Replacement

Notably ^ and $ match the ends of the entire string, not each line like in Regexp. Use /\A/ and /\z/ (There is also /\Z/ which matches before a final newline.)

The dot . does match a newline. Use /pattern/m.

To match a literal dot, replace a double backslash "\\." with a single backslash /\./

Literal brackets inside character classes need a backslash (gh#yast/yast-ruby-bindings#10)

Unfortunately regexp dialects are full of subtle differences like this. See http://www.regular-expressions.info/refflavors.html and do test.

Idiomatic Replacement

In a condition, use string =~ pattern which returns integer or nil.

Parameters:

  • string (String)

    a string to search

  • pattern (String)

    a regex in the C(!) syntax

Returns:

  • (Boolean, nil)

    does string match pattern



# File 'src/ruby/yast/builtins.rb', line 1000

.regexpsub(input, pattern, output) ⇒ String?

Searches a string for a POSIX Extended Regular Expression match and returns output with the matched subexpressions substituted or nil if no match was found.

If string or pattern is nil, or if pattern is an invalid regex, nil is returned. If output is nil or any other non-String, an Exception is raised.

Examples:

Usage

Builtins.regexpsub("lose", "(.*)s(.*)", "\\1v\\2")   # -> "love"

Usage misunderstood

Builtins.regexpsub("lose", "s", "v")                 # -> "v"

No match

Builtins.regexpsub("team", "I", "profit")            # -> nil

Parameters:

  • input (String)

    a string to search

  • pattern (String)

    a regex in the C(!) syntax

  • output (String)

    a template for what to return

Returns:

  • (String, nil)

    replacement, or no match

See Also:



# File 'src/ruby/yast/builtins.rb', line 1034

.remove(object, element) ⇒ Object?

Deprecated.

use native ruby method Hash#delete, Array#delete_at or Term#params (call delete_at on term params)

Remove element from a copy of object.

Parameters:

  • object (::Hash, ::Array, Yast::Term, nil)

    a container

  • element (Object, Integer, nil)

    Any key for Hash; Integer for Array and Term (negative means out of bounds!); terms are indexed from 1 (one)

Returns:

  • (Object, nil)

    Always returns a copy of object. The copy is unchanged if either object or element is nil, or if an Integer element is out of bounds. otherwise the key/index element is removed from the copy.

Raises:

  • (RuntimeError)

    if object has an unexpected type



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'src/ruby/yast/builtins.rb', line 160

def self.remove(object, element)
  return nil if object.nil?

  res = Yast.deep_copy(object)
  return res if element.nil?
  case object
  when ::Array
    return res if element < 0

    res.delete_at element
  when ::Hash
    res.delete element
  when Yast::Term
    return res if element < 1

    res.params.delete_at element - 1
  else
    raise "Invalid type passed to remove #{object.class}"
  end

  res
end

.search(string, substring) ⇒ Object

Deprecated.

use String#index instead

Returns position of a substring (nil if not found)



858
859
860
861
# File 'src/ruby/yast/builtins.rb', line 858

def self.search(string, substring)
  return nil if string.nil? || substring.nil?
  string.index substring
end

.select(object, element, default) ⇒ Object

Deprecated.

use native [] operator

  • Selects a list element (deprecated, use LIST[INDEX]:DEFAULT)
  • Select item from term


186
187
188
# File 'src/ruby/yast/builtins.rb', line 186

def self.select(object, element, default)
  Yast::Ops.get(object, [element], default)
end

.setcontains(list, value) ⇒ Object

Deprecated.

use Array#include?

setcontains() Yast built-in Checks if a sorted list contains an element



431
432
433
434
# File 'src/ruby/yast/builtins.rb', line 431

def self.setcontains(list, value)
  # simply call contains(), setcontains() is just optimized contains() call
  contains list, value
end

.setenv(env, value, overwrite = true) ⇒ Object

Deprecated.

use ENV#[]= instead

Change or add an environment variable



565
566
567
568
569
570
# File 'src/ruby/yast/builtins.rb', line 565

def self.setenv(env, value, overwrite = true)
  return true if ENV.include?(env) && !overwrite

  ENV[env] = value
  true
end

.sformat(format, *args) ⇒ Object

Yast compatible way how to format string with type conversion see tostring for type conversion



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'src/ruby/yast/builtins.rb', line 574

def self.sformat(format, *args)
  return nil if format.nil? || !format.is_a?(::String)

  return format if args.empty?

  format.gsub(/%./) do |match|
    case match
    when "%%"
      "%"
    when /%([1-9])/
      pos = Regexp.last_match(1).to_i - 1
      if pos < args.size
        tostring args[pos]
      else
        Yast.y2warning 1, "sformat: Illegal argument number #{match}, maximum is %#{args.size - 1}."
        ""
      end
    else
      Yast.y2warning 1, "sformat: Illegal argument number #{match}."
      ""
    end
  end
end

.size(object) ⇒ Object

Deprecated.

use builtin Array#size,Hash#size,Term#size,Path#size,String#size method

size() Yast built-in

  • Size of a map
  • Returns the number of path elements
  • Returns size of list
  • Returns the number of arguments of the term TERM.
  • Returns the number of characters of the string s


197
198
199
200
201
202
203
204
205
206
# File 'src/ruby/yast/builtins.rb', line 197

def self.size(object)
  return nil if object.nil?

  case object
  when ::String, ::Array, ::Hash, Yast::Term, Yast::Path
    return object.size
  else
    raise "Invalid object for size() builtin"
  end
end

.sleep(milisecs) ⇒ Object

Deprecated.

use Kernel#sleep instead. For miliseconds divide number by 1000.0.

Sleeps a number of milliseconds.



600
601
602
603
# File 'src/ruby/yast/builtins.rb', line 600

def self.sleep(milisecs)
  # ruby sleep() accepts seconds (float)
  ::Kernel.sleep milisecs / 1000.0
end

.sort(array, &block) ⇒ Object

Deprecated.

use Array#sort but be aware differences between ruby and old ycp sorting

sort() Yast built-in Sorts a List according to the Yast builtin predicate

See Also:

  • for details how it sorts


440
441
442
443
444
445
446
447
448
449
450
# File 'src/ruby/yast/builtins.rb', line 440

def self.sort(array, &block)
  return nil if array.nil?

  res = if block_given?
    array.sort { |x, y| block.call(x, y) ? -1 : 1 }
  else
    array.sort { |x, y| Yast::Ops.comparable_object(x) <=> y }
  end

  Yast.deep_copy(res)
end

.splitstring(string, sep) ⇒ Object

Deprecated.

use String#split but note that ycp version keep empty values in list

splitstring() Yast built-in Split a string by delimiter



455
456
457
458
459
460
461
# File 'src/ruby/yast/builtins.rb', line 455

def self.splitstring(string, sep)
  return nil if string.nil? || sep.nil?
  return [] if sep.empty?

  # the big negative value forces keeping empty values in the list
  string.split(/[#{Regexp.escape sep}]/, -1 * 2**20)
end

.srandom(param = nil) ⇒ Object

Deprecated.

use ruby native Kernel#srand

Initialize random number generator - srandom() Get the current random number generator seed - int srandom()



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'src/ruby/yast/builtins.rb', line 211

def self.srandom(param = nil)
  if param.nil?
    # be more secure here, original Yast uses Time.now with second precision
    # for seeding which is not secure enough, calling Ruby srand without
    # paramater causes to use time, PID and a sequence number for seeding
    # which is more secure
    srand

    # the original srandom() returns Time.now
    ::Time.now.to_i
  else
    srand param
    return nil
  end
end

.strftime(time, format) ⇒ String

Wrapper for the C++ version of strftime. Needed because it honors the system locale settings, something that is not granted by the Ruby version of strftime.

Parameters:

  • time (Time/DateTime)

    time to format

  • format (String)

    format string according to C++ strftime specification

Returns:

  • (String)

    formatted string

See Also:



685
686
687
# File 'src/ruby/yast/builtins.rb', line 685

def self.strftime(time, format)
  strftime_wrapper(time_to_tm(time), format)
end

.sublist(list, offset, length = DEF_LENGHT) ⇒ Object

Deprecated.

use Array#slice instead

Extracts a sublist

  • sublist(, )
  • sublist(, , )


469
470
471
472
473
474
475
476
477
# File 'src/ruby/yast/builtins.rb', line 469

def self.sublist(list, offset, length = DEF_LENGHT)
  return nil if list.nil? || offset.nil? || length.nil?

  length = list.size - offset if length == DEF_LENGHT
  return nil if offset < 0 || offset >= list.size
  return nil if length < 0 || offset + length > list.size

  Yast.deep_copy(list)[offset..offset + length - 1]
end

.substring(string, offset, length = -1)) ⇒ Object

Deprecated.

use String#[] instead

substring() Yast built-in Extracts a substring little bit complicated because Yast returns different values in corner cases (nil or negative parameters, out of range...)



868
869
870
871
872
873
874
875
# File 'src/ruby/yast/builtins.rb', line 868

def self.substring(string, offset, length = -1)
  return nil if string.nil? || offset.nil? || length.nil?
  return "" if offset < 0 || offset >= string.size

  length = string.size - offset if length < 0

  string[offset, length]
end

.symbolof(term) ⇒ Object

Deprecated.

use Term#value instead

Returns the symbol of the term TERM.



1071
1072
1073
1074
1075
# File 'src/ruby/yast/builtins.rb', line 1071

def self.symbolof(term)
  return nil if term.nil?

  term.value
end

.timeObject

Deprecated.

use ::Time.now.to_i instead

time() Yast built-in Return the number of seconds since 1.1.1970.



608
609
610
# File 'src/ruby/yast/builtins.rb', line 608

def self.time
  ::Time.now.to_i
end

.timestring(format, time, utc) ⇒ Object

Deprecated.

use Time#strftime instead

Returns time string



879
880
881
882
883
884
885
886
# File 'src/ruby/yast/builtins.rb', line 879

def self.timestring(format, time, utc)
  return nil if format.nil? || time.nil? || utc.nil?

  t = Time.at time
  t = t.utc if utc

  t.strftime format
end

.toascii(string) ⇒ Object

Gets new string including only characters below 0x7F



889
890
891
892
893
894
895
# File 'src/ruby/yast/builtins.rb', line 889

def self.toascii(string)
  return nil if string.nil?

  ret = ""
  string.each_char { |c| ret << c if c.ord < 0x7f }
  ret
end

.tobyteblockObject

Deprecated.

use different byte holder

Note:

not implmeneted as noone use it as far as we know

Converts a value to a byteblock.



250
251
252
# File 'src/ruby/yast/builtins.rb', line 250

def self.tobyteblock
  raise "Builtin tobyteblock() is not implemented yet"
end

.tofloat(value) ⇒ Object

Deprecated.

Use Object#to_f instead

Converts a value to a floating point number.



300
301
302
303
304
305
306
# File 'src/ruby/yast/builtins.rb', line 300

def self.tofloat(value)
  return nil if value.nil?

  return value.to_f
rescue
  return nil
end

.tohexstring(int, width = 0) ⇒ Object

Deprecated.

use Integer#to_s with base 16 instead but note that there is slight differences

Converts an integer to a hexadecimal string.

  • tohexstring()
  • tohexstring(, width)


901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
# File 'src/ruby/yast/builtins.rb', line 901

def self.tohexstring(int, width = 0)
  return nil if int.nil? || width.nil?

  if int >= 0
    format("0x%0#{width}x", int)
  else
    # compatibility for negative numbers
    # Ruby: -3 => '0x..fd'
    # Yast:  -3 => '0xfffffffffffffffd' (64bit integer)

    # this has '..fff' prefix
    ret = format("%018x", int)

    # pad with zeroes or spaces if needed
    if width > 16
      ret.insert(2, "0" * (width - 16))
    elsif width < -16
      ret << (" " * (-width - 16))
    end

    # replace the ".." prefix by "0x"
    ret[0..1] = "0x"
    ret
  end
end

.tointeger(object) ⇒ Object

Note:

recommended to replace by String#to_i but behavior is slightly different

Converts a value to an integer.



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'src/ruby/yast/builtins.rb', line 314

def self.tointeger(object)
  return nil if object.nil?

  case object
  when ::String
    # To be 100% Yast compatible we need to do this,
    # see https://github.com/yast/yast-core/blob/master/libycp/src/YCPInteger.cc#L39
    if object[0] == "0"
      object[1] == "x" ? object[2..-1].to_i(16) : object.to_i(8)
    else
      cleaned = object[/-?\d+/]
      cleaned and cleaned.to_i
    end
  # use full qualified ::Float to avoid clash with Yast::Builtins::Float
  when ::Float, ::Integer
    object.to_i
  end
  # else nil
end

.tolist(object) ⇒ Object

Deprecated.

not needed in ruby

Converts a value to a list (deprecated, use (list)VAR).



481
482
483
# File 'src/ruby/yast/builtins.rb', line 481

def self.tolist(object)
  object.is_a?(::Array) ? object : nil
end

.tolower(string) ⇒ Object

Deprecated.

use String#downcase instead

tolower() Yast built-in Makes a string lowercase



930
931
932
933
934
935
# File 'src/ruby/yast/builtins.rb', line 930

def self.tolower(string)
  return nil if string.nil?
  # cannot use String#downcase as it from ruby 2.4 convert also non-ascii so keep backward
  # compatible
  string.tr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")
end

.tomap(object) ⇒ Object

Deprecated.

not needed in ruby or use Hash.try_convert

Converts a value to a map.



535
536
537
# File 'src/ruby/yast/builtins.rb', line 535

def self.tomap(object)
  object.is_a?(::Hash) ? object : nil
end

.topath(object) ⇒ Object

Deprecated.

for conversion from String use directly Path methods

Converts a value to a path.



727
728
729
730
731
732
733
734
735
736
737
# File 'src/ruby/yast/builtins.rb', line 727

def self.topath(object)
  case object
  when Yast::Path
    return object
  when ::String
    object = "." + object unless object.start_with?(".")
    return Yast::Path.new(object)
  else
    return nil
  end
end

.toset(array) ⇒ Object

Deprecated.

use Set type or combination of #Array#sort and #Array#uniq

toset() Yast built-in Sorts list and removes duplicates



488
489
490
491
492
# File 'src/ruby/yast/builtins.rb', line 488

def self.toset(array)
  return nil if array.nil?
  res = array.uniq.sort { |x, y| Yast::Ops.comparable_object(x) <=> y }
  Yast.deep_copy(res)
end

.tostring(val, width = nil) ⇒ Object

Deprecated.

There is no strong reason to use this instead of inspect

Converts a value to a string in ycp.



939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
# File 'src/ruby/yast/builtins.rb', line 939

def self.tostring(val, width = nil)
  if width
    raise "tostring: negative 'width' argument: #{width}" if width < 0

    return format("%.#{width}f", val)
  end

  case val
  # string behavior depends if it is used inside something of alone
  when ::String then val
  when ::Symbol then "`#{val}"
  when ::Proc then "\"Annonymous method\""
  when Yast::YCode then "\"Remote code\""
  when ::NilClass then "nil"
  when ::TrueClass then "true"
  when ::FalseClass then "false"
  when ::Integer,
       ::Float,
       Yast::Term,
       Yast::Path,
       Yast::External,
       Yast::Byteblock
    val.to_s
  when ::Array then "[#{val.map { |a| inside_tostring(a) }.join(", ")}]"
  when ::Hash then "$[#{sort(val.keys).map { |k| "#{inside_tostring(k)}:#{inside_tostring(val[k])}" }.join(", ")}]"
  when Yast::FunRef
    # FIXME: Yast puts also the parameter names,
    # here the signature contains only data type without parameter name:
    #   Yast:    <YCPRef:boolean foo (string str, string str2)>
    #   Ruby:    <YCPRef:boolean foo (string, string)>
    #
    # There is also extra "any" in lists/maps:
    #   Yast:    <YCPRef:list <map> bar (list <map> a)>
    #   Ruby:    <YCPRef:list <map<any,any>> bar (list <map<any,any>>)>
    val.signature =~ /(.*)\((.*)\)/
    "<YCPRef:#{Regexp.last_match(1)}#{val.remote_method.name} (#{Regexp.last_match(2)})>"
  else
    y2warning 1, "tostring builtin called on wrong type #{val.class}"
    return val.inspect
  end
end

.tosymbol(value) ⇒ Object

Deprecated.

use #String#to_sym instead



1093
1094
1095
1096
1097
# File 'src/ruby/yast/builtins.rb', line 1093

def self.tosymbol(value)
  return nil if value.nil?

  value.to_sym
end

.toterm(symbol, list = DEF_LENGHT) ⇒ Object

Deprecated.

use Term constructor instead

Converts a value to a term.



1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'src/ruby/yast/builtins.rb', line 1079

def self.toterm(symbol, list = DEF_LENGHT)
  return nil if symbol.nil? || list.nil?

  case symbol
  when ::String
    Yast::Term.new(symbol.to_sym)
  when ::Symbol
    list == DEF_LENGHT ? Yast::Term.new(symbol) : Yast::Term.new(symbol, *list)
  when Yast::Term
    symbol
  end
end

.toupper(string) ⇒ Object

Deprecated.

use String#upcase instead

toupper() Yast built-in Makes a string uppercase



989
990
991
992
993
994
# File 'src/ruby/yast/builtins.rb', line 989

def self.toupper(string)
  return nil if string.nil?
  # cannot use String#upcase as it from ruby 2.4 convert also non-ascii so keep backward
  # compatible
  string.tr("abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
end

.union(first, second) ⇒ Object

Deprecated.

Use ruby builtins Hash#merge and #Array#|

  • Unions of lists
  • Union of 2 maps


230
231
232
233
234
235
236
237
238
239
240
241
# File 'src/ruby/yast/builtins.rb', line 230

def self.union(first, second)
  return nil if first.nil? || second.nil?

  case first
  when ::Array
    return Yast.deep_copy(first) | Yast.deep_copy(second)
  when ::Hash
    return first.merge(second)
  else
    raise "union builtin called on wrong type #{first.class}"
  end
end

.y2debug(*args) ⇒ Object

Deprecated.

Use Logger instead

Log a message to the y2log.



614
615
616
617
# File 'src/ruby/yast/builtins.rb', line 614

def self.y2debug(*args)
  shift_frame_number args
  Yast.y2debug(*args)
end

.y2error(*args) ⇒ Object

Deprecated.

Use Logger instead

Log an error to the y2log.



621
622
623
624
# File 'src/ruby/yast/builtins.rb', line 621

def self.y2error(*args)
  shift_frame_number args
  Yast.y2error(*args)
end

.y2internal(*args) ⇒ Object

Deprecated.

Use Logger instead

Log an internal message to the y2log.



628
629
630
631
# File 'src/ruby/yast/builtins.rb', line 628

def self.y2internal(*args)
  shift_frame_number args
  Yast.y2internal(*args)
end

.y2milestone(*args) ⇒ Object

Deprecated.

Use Logger instead

Log a milestone to the y2log.



635
636
637
638
# File 'src/ruby/yast/builtins.rb', line 635

def self.y2milestone(*args)
  shift_frame_number args
  Yast.y2milestone(*args)
end

.y2security(*args) ⇒ Object

Deprecated.

Use Logger instead

Log a security message to the y2log.



642
643
644
645
# File 'src/ruby/yast/builtins.rb', line 642

def self.y2security(*args)
  shift_frame_number args
  Yast.y2security(*args)
end

.y2useritem(*_args) ⇒ Object

Note:

do nothing now, concept is quite unclear

Log an user-level system message to the y2changes



665
666
667
668
# File 'src/ruby/yast/builtins.rb', line 665

def self.y2useritem(*_args)
  # TODO: implement it
  nil
end

.y2usernote(*_args) ⇒ Object

Note:

do nothing now, concept is quite unclear

Log an user-level addional message to the y2changes



672
673
674
675
# File 'src/ruby/yast/builtins.rb', line 672

def self.y2usernote(*_args)
  # TODO: implement it
  nil
end

.y2warning(*args) ⇒ Object

Deprecated.

Use Logger instead

Log a warning to the y2log.



649
650
651
652
# File 'src/ruby/yast/builtins.rb', line 649

def self.y2warning(*args)
  shift_frame_number args
  Yast.y2warning(*args)
end