Class: Plotty::Function

Inherits:
Struct
  • Object
show all
Defined in:
lib/plotty/graph.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



58
59
60
# File 'lib/plotty/graph.rb', line 58

def command
  @command
end

#patternObject

Returns the value of attribute pattern

Returns:

  • (Object)

    the current value of pattern



58
59
60
# File 'lib/plotty/graph.rb', line 58

def pattern
  @pattern
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



58
59
60
# File 'lib/plotty/graph.rb', line 58

def title
  @title
end

Class Method Details

.parse(pattern, command) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/plotty/graph.rb', line 59

def self.parse(pattern, command)
	pattern = Regexp.new(pattern)
	
	if command =~ /^(\w+):(.*?)$/
		self.new(pattern, $2, $1)
	else
		self.new(pattern, command, command)
	end
end

Instance Method Details

#call(value) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/plotty/graph.rb', line 69

def call(value)
	r, w = IO.pipe
	
	# puts "Running #{@command} with x = #{value}..."
	
	pid = Process.spawn({'x' => value.to_s}, self.command, out: w, err: STDERR)
	
	w.close
	
	buffer = r.read
	
	Process.waitpid pid
	
	if match = self.pattern.match(buffer)
		result = match[1] || match[0]
		
		# puts "\tresult = #{result}"
		
		return result
	end
end