Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add english name for $_ and $F #31

Merged
merged 2 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ Style/SingleLineMethods:
Style/StderrPuts:
Enabled: false

Style/GlobalVars:
Exclude:
- mrblib/rf/container.rb

Style/SpecialGlobalVars:
Exclude:
- mrblib/rf/container.rb

Security/YAMLLoad:
Enabled: false

Expand All @@ -43,6 +51,14 @@ Naming/MemoizedInstanceVariableName:
Exclude:
- mrblib/rf/container.rb

Naming/MethodName:
Exclude:
- mrblib/rf/container.rb

Naming/VariableName:
Exclude:
- mrblib/rf/container.rb

RSpec/ContextWording:
Prefixes:
- when
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,10 @@ In rf, you can use [special variables](https://docs.ruby-lang.org/ja/latest/clas

| Variable name | Description |
|-------|------|
| \_ | The input record |
| $\_ | Same as \_ |
| $F | It is an array that stores fields(`$F=_.split`) |
| \_0 | Same as \_ |
| record, \_, $\_, \_0 | The input record |
| fields, $F | It is an array that stores fields(`$F=_.split`) |
| \_1, \_2, \_3, ... | First field, second field, third field... |
| $. | The number of records loaded(1-indexed) |
| @NR | Same as $. |
| $., @NR | The number of records loaded(1-indexed) |

## Built-in methods

Expand Down
9 changes: 3 additions & 6 deletions README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,10 @@ rfではRubyで定義されている[特殊変数](https://docs.ruby-lang.org/ja

| 変数名 | 説明 |
|-------|------|
| \_ | 入力されたレコードです |
| $\_ | \_ のエイリアスです |
| $F | フィールドを格納した配列です(`$F=_.split`) |
| \_0 | \_ と等価です |
| record, \_, $\_, \_0 | 入力されたレコードです |
| fields, $F | フィールドを格納した配列です(`fields=_.split`) |
| \_1, \_2, \_3, ... | 1番目、2番目、3番目のフィールドです |
| $. | 1始まりの読み込んだレコード数です |
| @NR | $.と同じです |
| $., @NR | 1始まりの読み込んだレコード数です |

## 組み込みメソッド

Expand Down
2 changes: 1 addition & 1 deletion mrblib/rf/00filter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Base
attr_reader :data, :record, :index, :fields

def each_record
index = 0
index = 1
data.each do |record|
@record = preprocess(record)
@index = index
Expand Down
10 changes: 4 additions & 6 deletions mrblib/rf/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ def pre_action

def do_action
filter.each_record do |record, index, fields|
container._ = record
container.NR = $. = index + 1
$F = fields # rubocop:disable Style/GlobalVars
container.record = record
container.NR = index
container.fields = fields

ret = bind.eval(command)
next if config.quiet

filter.output(ret)
filter.output(ret) unless config.quiet
end
end

Expand Down
23 changes: 20 additions & 3 deletions mrblib/rf/container.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
module Rf
class Container
def _
$_ # rubocop:disable Style/SpecialGlobalVars
$_
end

def _=(data)
$_ = data
end

attr_accessor :NR # rubocop:disable Naming/MethodName
alias record _
alias record= _=

def fields
$F
end

def fields=(data)
$F = data
end

def NR
@NR ||= 1
end

def NR=(num)
@NR = $. = num
end

def string?
_.instance_of?(String)
Expand Down Expand Up @@ -41,7 +58,7 @@ def method_missing(sym, *)
if sym == :_0
_
elsif s =~ /\A_[1-9]\d*\z/
$F[s[1..].to_i - 1] # rubocop:disable Style/GlobalVars
$F[s[1..].to_i - 1]
else
super
end
Expand Down
188 changes: 82 additions & 106 deletions spec/special_variables_spec.rb
Original file line number Diff line number Diff line change
@@ -1,133 +1,109 @@
describe 'Special Variables' do
describe '$_' do
let(:input) { 'foo' }
let(:output) { input }

before { run_rf('-q "puts $_"', input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
context 'for record variables' do
where(:name) do
%w[record $_ _ _0]
end

describe '_' do
let(:input) { 'foo' }
let(:output) { input }
with_them do
let(:input) { 'foo' }
let(:output) { input }

before { run_rf('-q "puts _"', input) }
before { run_rf("-q 'puts #{name}'", input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end

describe '_0' do
let(:input) { 'foo' }
let(:output) { input }

before { run_rf('-q "puts _0"', input) }
context 'for fields variables' do
where(:name) do
%w[fields $F]
end

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
with_them do
context 'when record is String' do
let(:input) { 'foo bar baz' }
let(:output) do
<<~OUTPUT
"foo"
"bar"
"baz"
OUTPUT
end

before { run_rf('-q "p $F[0],$F[1],$F[2]"', input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end

describe '$F' do
context 'when record is String' do
let(:input) { 'foo bar baz' }
let(:output) do
<<~OUTPUT
"foo"
"bar"
"baz"
OUTPUT
context 'when record is Hash' do
let(:input) do
<<~JSON
{
"a": 1,
"b": 2,
"c": 3
}
JSON
end
let(:output) do
<<~OUTPUT
["a", 1]
["b", 2]
["c", 3]
OUTPUT
end

before { run_rf("-j -q 'p #{name}[0],#{name}[1],#{name}[2]'", input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end

before { run_rf('-q "p $F[0],$F[1],$F[2]"', input) }
context 'when record is Other class' do
let(:input) { '1' }
let(:output) do
<<~OUTPUT
1
nil
nil
OUTPUT
end

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
before { run_rf("-j -q 'p #{name}[0],#{name}[1],#{name}[2]'", input) }

context 'when record is Hash' do
let(:input) do
<<~JSON
{
"a": 1,
"b": 2,
"c": 3
}
JSON
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
let(:output) do
<<~OUTPUT
["a", 1]
["b", 2]
["c", 3]
OUTPUT
end

before { run_rf('-j -q "p $F[0],$F[1],$F[2]"', input) }
end
end

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
context 'for number of records' do
where(:name) do
%w[$. NR]
end

context 'when record is Other class' do
let(:input) { '1' }
with_them do
let(:input) do
<<~TEXT
foo
bar
baz
TEXT
end
let(:output) do
<<~OUTPUT
1
nil
nil
1 foo
2 bar
3 baz
OUTPUT
end

before { run_rf('-j -q "p $F[0],$F[1],$F[2]"', input) }
before { run_rf(%q/'[$.,_].join(" ")'/, input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end

describe '$.' do
let(:input) do
<<~TEXT
foo
bar
baz
TEXT
end
let(:output) do
<<~OUTPUT
1 foo
2 bar
3 baz
OUTPUT
end

before { run_rf(%q/'[$.,_].join(" ")'/, input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end

describe '@NR' do
let(:input) do
<<~TEXT
foo
bar
baz
TEXT
end
let(:output) do
<<~OUTPUT
1 foo
2 bar
3 baz
OUTPUT
end

before { run_rf(%q/'[@NR,_].join(" ")'/, input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end