Module: Kernel
- Defined in:
- lib/shenanigans/kernel/fn.rb,
lib/shenanigans/kernel/with.rb,
lib/shenanigans/kernel/prompt.rb
Constant Summary collapse
- CONVERSIONS =
Currently only used by
prompt
::to_i
,:to_f
,:to_r
,:to_sym
,:to_c
[:to_i, :to_f, :to_r, :to_sym, :to_c]
Instance Method Summary collapse
-
#fn(*funs) ⇒ Object
Composes a list of functions.
-
#prompt(text = '', conversion = nil) ⇒ Object
Displays a prompt and returns chomped input.
-
#with(o, &blk) ⇒ Object
A Pascal/ActionScript like
with
method.
Instance Method Details
#fn(*funs) ⇒ Object
Composes a list of functions. Functions can be specified as symbols or lambdas.
["foo bar", "baz qux"].map &fn(:split, :last)
#=> ["bar", "qux"]
(1..3).map &fn(:next, -> x { x * x }, -> x { x.to_f / 2 } )
#=> [2.0, 4.5, 8.0]
9 10 11 12 13 14 15 |
# File 'lib/shenanigans/kernel/fn.rb', line 9 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) ⇒ Object
Displays a prompt and returns chomped input. Modelled after the Python method raw_input
, but also can be supplied with an optional conversion method.
prompt("Prompt> ")
Prompt> 12
#=> "12"
prompt("Prompt> ", :to_f)
Prompt> 12
#=> 12.0
17 18 19 20 21 |
# File 'lib/shenanigans/kernel/prompt.rb', line 17 def prompt(text='', conversion=nil) print text unless text.empty? input = gets.chomp CONVERSIONS.include?(conversion) ? input.send(conversion) : input end |
#with(o, &blk) ⇒ Object
A Pascal/ActionScript like with
method. Yields its argument to the provided block and then returns it.
with([]) do |a|
a << "a"
a << "b"
end
#=> ["a", "b"]
9 10 11 |
# File 'lib/shenanigans/kernel/with.rb', line 9 def with(o, &blk) o.tap(&blk) end |