Ruby common file operation code example

Source: Internet
Author: User

Ruby common file operation code example

This article mainly introduces common Ruby file operation code instances, such as file creation, output file content, IO operations, output file path, and stringio usage. For more information, see

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

# Create a 222. rb file and input characters

File = File. open ("222.rb"," w + ")

File. puts "123 \ nwadwa \ n12124124 \ ndwdw"

File. close

 

 

# Output 222. rb content

File. open ("222.rb"," r + ") do | file |

While line = file. gets

Puts line

End

End

 

 

# Directly using IO to operate files

IO. foreach ("222.rb") do | line |

Puts line if line = ~ /Abc/# The output matches the row of 'abc '.

Puts line if line !~ /Qwe/# The output does not match the row where 'qw' is located.

End

 

 

 

# Absolute path of the output file

Puts File. expand_path ("222.rb ")

 

 

# Count chars from a file

File = File. new ("222.rb ")

W_count = 0

File. each_byte do | byte |

W_count + = 1 if byte =? 1

End

Puts "# {w_count }"

 

 

# Create new file and write some words there

Print "The file now is exist? -->"

Puts File. exist? ("Asd.txt") # determine whether a file exists

 

File = File. new ("asd.txt", "w ")

Print "The file now is exist? -->"

Puts File. exist? ("Asd.txt ")

 

File. write ("hehe \ nhahah ")

 

 

# Io. stream operation

Require 'stringio'

Ios = StringIO. new ("abcdef \ n ABC \ n 12345 ")

Ios. seek (5) # Move the offset pointer to 5 (where the e letter is located)

Ios. puts ("xyz3") # overwrite the original data from 5

Puts ios. tell # tell -- Returns the current offset (in bytes) of ios.

Puts ios. string

Puts ios. string. dump # ignore the escape of \ n

 

 

# Another example

Require 'stringio'

Ios = StringIO. new ("abcdef \ nq9ert \ n 12345 ")

Ios. seek (3)

Ios. ungetc (? W) # replace the char at index 3

Puts "Ptr =#{ ios. tell }"

S1 = ios. gets # filte the "\ n"

S2 = ios. gets

Puts s1

Puts s2

 

 

# Ruby open a file and write data

Txt = File. open ("File path", "w + ")

Txt. puts 'content of the file to be written'

Txt. close

# Reading data from a file

Num = File. readlines ("File path") [0]. chomp

# How to open a file

System ("notepad file path ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.