Class: Faker::Source

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/source.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.hello_world(legacy_lang = NOT_GIVEN, lang: :ruby) ⇒ String

Produces source code for Hello World in a given language.

Examples:

Faker::Source.hello_world #=> "puts 'Hello World!'"
Faker::Source.hello_world(lang: :javascript)
  #=> "alert('Hello World!');"

Parameters:

  • lang (Symbol) (defaults to: :ruby)

    The programming language to use

Returns:

  • (String)

Available since:

  • 1.9.0


20
21
22
23
24
25
26
# File 'lib/faker/default/source.rb', line 20

def hello_world(legacy_lang = NOT_GIVEN, lang: :ruby)
  warn_for_deprecated_arguments do |keywords|
    keywords << :lang if legacy_lang != NOT_GIVEN
  end

  fetch("source.hello_world.#{lang}")
end

Produces source code for printing a string in a given language.

Examples:

Faker::Source.print #=> "puts 'faker_string_to_print'"
Faker::Source.print(str: 'foo bar', lang: :javascript)
  #=> "console.log('foo bar');"

Parameters:

  • str (String) (defaults to: 'some string')

    The string to print

  • lang (Symbol) (defaults to: :ruby)

    The programming language to use

Returns:

  • (String)

Available since:

  • 1.9.0


42
43
44
45
46
47
48
49
50
51
# File 'lib/faker/default/source.rb', line 42

def print(legacy_str = NOT_GIVEN, legacy_lang = NOT_GIVEN, str: 'some string', lang: :ruby)
  warn_for_deprecated_arguments do |keywords|
    keywords << :str if legacy_str != NOT_GIVEN
  end
  warn_for_deprecated_arguments do |keywords|
    keywords << :lang if legacy_lang != NOT_GIVEN
  end
  code = fetch("source.print.#{lang}")
  code.gsub('faker_string_to_print', str)
end

Produces source code for printing 1 through 10 in a given language.

Examples:

Faker::Source.print_1_to_10 #=> "(1..10).each { |i| puts i }"
Faker::Source.print_1_to_10(lang: :javascript)
# => "for (let i=0; i<10; i++) {
#       console.log(i);
#    }"

Parameters:

  • lang (Symbol) (defaults to: :ruby)

    The programming language to use

Returns:

  • (String)

Available since:

  • 1.9.0


68
69
70
71
72
73
# File 'lib/faker/default/source.rb', line 68

def print_1_to_10(legacy_lang = NOT_GIVEN, lang: :ruby)
  warn_for_deprecated_arguments do |keywords|
    keywords << :lang if legacy_lang != NOT_GIVEN
  end
  fetch("source.print_1_to_10.#{lang}")
end