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

#41
One trick I like is to use the splat (`*`) expander on objects other than Arrays. Here's an example on a regular expression match:

match, text, number = *"Something 981".match(/([A-z]*) ([0-9]*)/)

Other examples include:

a, b, c = *('A'..'Z')

Job = Struct.new(:name, :occupation)
tom = Job.new("Tom", "Developer")
name, occupation = *tom
Reply

#42
To combine multiple regexes with `|`, you can use

Regexp.union /Ruby\d/, /test/i, "cheat"

to create a Regexp similar to:

/(Ruby\d|[tT][eE][sS][tT]|cheat)/
Reply

#43
private unless Rails.env == 'test'
# e.g. a bundle of methods you want to test directly
Looks like a cool and (in some cases) nice/useful hack/feature of Ruby.
Reply

#44
Another tiny feature - convert a `Fixnum` into any base up to 36:

>> 1234567890.to_s(2)
=> "1001001100101100000001011010010"

>> 1234567890.to_s(8)
=> "11145401322"

>> 1234567890.to_s(16)
=> "499602d2"

>> 1234567890.to_s(24)
=> "6b1230i"

>> 1234567890.to_s(36)
=> "kf12oi"

And as Huw Walters has commented, converting the other way is just as simple:

>> "kf12oi".to_i(36)
=> 1234567890
Reply

#45
One of the cool things about ruby is that you can call methods and run code in places other languages would frown upon, such as in method or class definitions.

For instance, to create a class that has an unknown superclass until run time, i.e. is random, you could do the following:

class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample

end

RandomSubclass.superclass # could output one of 6 different classes.

This uses the 1.9 `Array#sample` method (in 1.8.7-only, see `Array#choice`), and the example is pretty contrived but you can see the power here.

Another cool example is the ability to put default parameter values that are non fixed (like other languages often demand):

def do_something_at(something, at = Time.now)
# ...
end

Of course the problem with the first example is that it is evaluated at definition time, not call time. So, once a superclass has been chosen, it stays that superclass for the remainder of the program.

However, in the second example, each time you call `do_something_at`, the `at` variable will be the time that the method was called (well, very very close to it)
Reply

#46
I just read all the answers... one notable omission was destructuring assignment:

> (a,b),c = [[1,2],3]
=> [[1,2],3]
> a
=> 1

It also works for block parameters. This is useful when you have nested arrays, each element of which represents something distinct. Instead of writing code like "array[0][1]", you can break that nested array down and give a descriptive name to each element, in a single line of code.
Reply

#47
The sprintf shortcut
--------------------

My favourite ruby feature. Syntax is `format_string % argument`

"%04d" % 1 # => "0001"
"%0.2f" % Math::PI # => "3.14"

Works as well for arrays (`format_string % array_of_arguments`)

"%.2f %.3f %.4f" % ([Math::PI]*3)
# => "3.14 3.142 3.1416"


Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

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