module Kernel

Constants

CONVERSIONS

Currently only used by prompt: :to_i, :to_f, :to_r, :to_sym, :to_c

Private Instance Methods

fn(*funs) click to toggle source

Composes a list of functions. Functions can be specified as symbols or lambdas.

@example Composing symbols

["foo bar", "baz qux"].map(&fn(:split, :last)) #=> ["bar", "qux"]

@example Composing symbplds and lambdas

(1..3).map(&fn(:next, -> x { x * x }, -> x { x.to_f / 2 } ))
#=> [2.0, 4.5, 8.0]
# File lib/shenanigans/kernel/fn.rb, line 10
def fn(*funs)
  ->(x) do
    funs.inject(x) do |v, f|
      Proc === f ? f.call(v) : v.send(f)
    end
  end
end
prompt(text = "", conversion = nil) click to toggle source

Displays a prompt and returns chomped input. Modelled after the Python method raw_input, but also can be supplied with an optional conversion method.

@example A simple prompt

prompt("Prompt> ")
Prompt> 12 #=> "12"

@example A prompt with conversion

prompt("Prompt> ", :to_f)
Prompt> 12 #=> 12.0
# File lib/shenanigans/kernel/prompt.rb, line 16
def prompt(text = "", conversion = nil)
  print text unless text.empty?
  input = gets.chomp
  CONVERSIONS.include?(conversion) ? input.send(conversion) : input
end
require_optional(gem, &block) click to toggle source

Optionally require a gem. If it is not available, nil will be returned. Alternatively, a block can be provided with code to run.

@example Without a block

require "non_existent" #=> nil

@example With a custom block

require "non_existent" do
  puts "Something went wrong"
end #=> Outputs "Something went wrong"
# File lib/shenanigans/kernel/require_optional.rb, line 12
def require_optional(gem, &block)
  require gem
rescue LoadError
  block&.call
end
with(o, &blk) click to toggle source

A Pascal/ActionScript like with method. Yields its argument to the provided block and then returns it.

@example

with([]) do |a|
  a << "a"
  a << "b"
end #=> ["a", "b"]
# File lib/shenanigans/kernel/with.rb, line 10
def with(o, &blk)
  o.tap(&blk)
end