Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 316 Vote(s) - 3.34 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hidden features of Ruby

#21
Use a Range object as an infinite lazy list:

Inf = 1.0 / 0

(1..Inf).take(5) #=> [1, 2, 3, 4, 5]


More info here:

[To see links please register here]

Reply

#22
class A

private

def my_private_method
puts 'private method called'
end
end

a = A.new
a.my_private_method # Raises exception saying private method was called
a.send :my_private_method # Calls my_private_method and prints private method called'
Reply

#23
James A. Rosen's tip is cool ([*items].each), but I find that it destroys hashes:

irb(main):001:0> h = {:name => "Bob"}
=> {:name=>"Bob"}
irb(main):002:0> [*h]
=> [[:name, "Bob"]]

I prefer this way of handling the case when I accept a list of things to process but am lenient and allow the caller to supply one:

irb(main):003:0> h = {:name => "Bob"}
=> {:name=>"Bob"}
irb(main):004:0> [h].flatten
=> [{:name=>"Bob"}]

This can be combined with a method signature like so nicely:

def process(*entries)
[entries].flatten.each do |e|
# do something with e
end
end
Reply

#24
The "ruby" binary (at least MRI's) supports a lot of the switches that made perl one-liners quite popular.

Significant ones:

- -n Sets up an outer loop with just "gets" - which magically works with given filename or STDIN, setting each read line in $_
- -p Similar to -n but with an automatic `put`s at the end of each loop iteration
- -a Automatic call to .split on each input line, stored in $F
- -i In-place edit input files
- -l Automatic call to .chomp on input
- -e Execute a piece of code
- -c Check source code
- -w With warnings

Some examples:

# Print each line with its number:
ruby -ne 'print($., ": ", $_)' < /etc/irbrc

# Print each line reversed:
ruby -lne 'puts $_.reverse' < /etc/irbrc

# Print the second column from an input CSV (dumb - no balanced quote support etc):
ruby -F, -ane 'puts $F[1]' < /etc/irbrc

# Print lines that contain "eat"
ruby -ne 'puts $_ if /eat/i' < /etc/irbrc

# Same as above:
ruby -pe 'next unless /eat/i' < /etc/irbrc

# Pass-through (like cat, but with possible line-end munging):
ruby -p -e '' < /etc/irbrc

# Uppercase all input:
ruby -p -e '$_.upcase!' < /etc/irbrc

# Same as above, but actually write to the input file, and make a backup first with extension .bak - Notice that inplace edit REQUIRES input files, not an input STDIN:
ruby -i.bak -p -e '$_.upcase!' /etc/irbrc

Feel free to google "ruby one-liners" and "perl one-liners" for tons more usable and practical examples. It essentially allows you to use ruby as a fairly powerful replacement to awk and sed.
Reply

#25
**Auto-vivifying hashes in Ruby**

def cnh # silly name "create nested hash"
Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
end
my_hash = cnh
my_hash[1][2][3] = 4
my_hash # => { 1 => { 2 => { 3 =>4 } } }

This can just be damn handy.
Reply

#26
`Fixnum#to_s(base)` can be really useful in some case. One such case is generating random (pseudo)unique tokens by converting random number to string using base of 36.

Token of length 8:

rand(36**8).to_s(36) => "fmhpjfao"
rand(36**8).to_s(36) => "gcer9ecu"
rand(36**8).to_s(36) => "krpm0h9r"

Token of length 6:

rand(36**6).to_s(36) => "bvhl8d"
rand(36**6).to_s(36) => "lb7tis"
rand(36**6).to_s(36) => "ibwgeh"
Reply

#27
Boolean operators on non boolean values.

`&&` and `||`

Both return the value of the last expression evaluated.

Which is why the `||=` will update the variable with the value returned expression on the right side if the variable is undefined. This is not explicitly documented, but common knowledge.

However the `&&=` isn't quite so widely known about.

string &&= string + "suffix"

is equivalent to

if string
string = string + "suffix"
end

It's very handy for destructive operations that should not proceed if the variable is undefined.
Reply

#28
@user #=> nil (but I did't know)
@user.name rescue "Unknown"

Reply

#29
Wow, no one mentioned the flip flop operator:

1.upto(100) do |i|
puts i if (i == 3)..(i == 15)
end
Reply

#30
**each_with_index** method for any enumarable object ( array,hash,etc.) perhaps?


myarray = ["la", "li", "lu"]
myarray.each_with_index{|v,idx| puts "#{idx} -> #{v}"}

#result:
#0 -> la
#1 -> li
#2 -> lu


Maybe it's more well known than other answers but not that well known for all ruby programmers :)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through